"Android" does not operate pop-up "screensaver" effect for some time

Source: Internet
Author: User

Imitation screensaver effect of the recent project a small demo, and you share, this article is mainly to show a copy of the effect of screensavers, users in a period of time do not operate will automatically pop-up screen saver page, when in the screen saver page, "Two seconds consecutive" press two times keyboard "enter" will exit the screensaver page, as follows:

Here is a concrete introduction to the implementation process:

One, 5 seconds does not operate the automatic lock screen interface

This interface is very simple, just a textview, directly paste code:

<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools"android:layout_width="Match_ Parent "android:layout_height="match_parent "tools:context=". Mainactivity " >                      <TextView          Android:id="@+id/start"         Android:layout_width="Match_parent"         Android:layout_height="Wrap_content"         android:layout_centerinparent="true"         android:textsize="30SP"         android:gravity="Center"         Android:textstyle="Bold"         Android:textcolor="#698048"         Android:text="5 seconds do not operate automatic lock screen"/></relativelayout>

Ii. Concrete realization of logic

First look at the Mainpresenter class, is the timing of the specific implementation of the logic class, mainly through the runnable to achieve the operation, when no operation 5 seconds after the execution of Runnable, jump to the lock screen interface, 5 seconds within the operation will reset the timing, the specific code is as follows:

 Public  class mainpresenter{     Public Final Static intMsg_show_tips =0x01;PrivateImainview Mmainview;PrivateMainHandler Mmainhandler;Private Booleantipsisshowed =true;PrivateRunnable tipsshowrunable =NewRunnable () {@Override         Public void Run() {mmainhandler.obtainmessage (msg_show_tips). Sendtotarget (); }    }; Public Mainpresenter(Imainview view)        {Mmainview = view; Mmainhandler =NewMainHandler (); }/** * * < start timing without action > * < function Details > * * @see [Class, Class # method, Class # Member] * /     Public void Starttipstimer() {mmainhandler.postdelayed (tipsshowrunable, the); }/** * * < end current timings, reset timings > * < feature details > * * @see [Class, Class # method, Class # Member] */     Public void Endtipstimer() {mmainhandler.removecallbacks (tipsshowrunable); } Public void Resettipstimer() {tipsisshowed =false;        Mmainhandler.removecallbacks (tipsshowrunable); Mmainhandler.postdelayed (Tipsshowrunable, the); } Public  class mainhandler extends Handler {        @Override         Public void Handlemessage(Message msg) {Super. Handlemessage (msg);Switch(msg.what) { CaseMSG_SHOW_TIPS:mMainView.showTipsView (); tipsisshowed =true;//screensavers display, press the keyboard enter button continuously in two seconds to turn off Screensavers                     Break; }        }    }}

Third, Mainactivity.java is the main page

The main function is to override the OnKeyDown method, listen to the keyboard press event, once the keyboard event is triggered, the timing will be reset, in addition to the Onresume () method, the default start time, Showtipsview () method is timed to complete the popup screen saver interface.

 Public  class mainactivity extends Activity implements Imainview{     PrivateMainpresenter Mpresenter;protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Mpresenter =NewMainpresenter ( This); }@Override     Public Boolean OnKeyDown(intKeyCode, KeyEvent event) {//Reset TimingMpresenter.resettipstimer ();return false; }@Override    protected void Onresume()    {//start default start TimerMpresenter.starttipstimer ();Super. Onresume (); }@Override    protected void OnPause()    {//End timing when other operations are in actionMpresenter.endtipstimer ();Super. OnPause (); }@Override     Public void Showtipsview()    {//Display screen saver screenIntent Intent =NewIntent (mainactivity. This, Mytimeactivity.class);    StartActivity (Intent); }}

In addition, you see mainactivity implementation of the Imainview interface, Imainview is very simple, encapsulated a Showtipsview () method, mainactivity implementation of the method, Imainview.java code as follows.

publicinterface IMainView{    publicvoidshowTipsView();}

Four, screen saver interface Mytimeactivity.java

Mytimeactivity interface is very simple, is a background map, plus a system time display, there is a two second consecutive press ENTER will exit the screen saver interface logic, the specific code is as follows, the key parts are added comments:

 Public  class mytimeactivity extends Activity{    Private Static Final intMsgkey =0x10001;Private LongExittime =0;//Screensaver in the upper right corner of the system time    PrivateTextView MTime;@Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.lock_screen); MTime = (TextView) Findviewbyid (r.id.mytime);//Startup thread refreshes the upper-right corner of the screen saver screen        NewTimethread (). Start (); } Public  class timethread extends Thread {        @Override         Public void Run() {do {Try{//Update TimeThread.Sleep ( +); Message msg =NewMessage ();                    Msg.what = Msgkey;                Mhandler.sendmessage (msg); }Catch(Interruptedexception e)                {E.printstacktrace (); }            } while(true); }    }/** * 2 Second consecutive press ENTER to exit Screensaver * Override method * @param keycode * @param event * @return * @see android.app.activity#onkeydown (int, android.view.KeyEvent) */    @Override     Public Boolean OnKeyDown(intKeyCode, KeyEvent event) {if(keycode = = Keyevent.keycode_enter) {exit ();return false; }return Super. OnKeyDown (KeyCode, event); } Public void Exit()    {if((System.currenttimemillis ()-Exittime) > -) {Toast.maketext (Getapplicationcontext (),"Press the Unlock key again to enter the application", Toast.length_short). Show ();        Exittime = System.currenttimemillis (); }Else{Finish (); }    }PrivateHandler Mhandler =NewHandler () {@Override         Public void Handlemessage(Message msg) {Super. Handlemessage (msg);Switch(msg.what) { CaseMsgkey:LongSysTime = System.currenttimemillis (); Charsequence systimestr = Dateformat.format ("hh:mm", sysTime); Mtime.settext (SYSTIMESTR); Break; }        }    };}
So far, the effect of imitation screensaver has been completed, a very simple example, welcome criticism, common learning, common progress!

"Android" does not operate pop-up "screensaver" effect for some time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.