Powermanager instance 1 (on)

Source: Internet
Author: User
File 1 mainactivity. java: Import android. app. activity; import android. content. context; import android. content. res. resources; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. OS. powermanager; import android. OS. systemclock; import android. util. log; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. tex Tview; public class mainactivity extends activity implements onclicklistener {/** called when the activity is first created. */Final Static int kstatekey = R. id. statekey; Final Static int ktextturnonkey = R. id. textturnonkey; Final Static int ktextturnoffkey = R. id. textturnoffkey; Final Static string kstateoff = "off"; Final Static string kstateon = "on"; powermanager PM; powermanager. wakelock Wl; Final Static stri Ng tag = "Robin"; handler = new myhandler (); @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button BTN = NULL; resources res = This. getresources (); For (INT I = R. id. button0; I <= R. id. button5; I ++) {BTN = (button) findviewbyid (I); BTN. settag (kstatekey, kstateoff); if (I = R. id. button0) {BTN. settag (ktextturnonkey, Res. getstring (R. String. acquire_partial); BTN. settag (ktextturnoffkey, Res. getstring (R. string. release_partial);} else if (I = R. id. button1) {BTN. settag (ktextturnonkey, Res. getstring (R. string. acquire_screen_dim); BTN. settag (ktextturnoffkey, Res. getstring (R. string. release_screen_dim);} else if (I = R. id. button2) {BTN. settag (ktextturnonkey, Res. getstring (R. string. acquire_screen_bright); BTN. settag (ktextturnoffkey, re S. getstring (R. string. release_screen_bright);} else if (I = R. id. button3) {BTN. settag (ktextturnonkey, Res. getstring (R. string. acquire_full_wake_lock); BTN. settag (ktextturnoffkey, Res. getstring (R. string. release_full_wake_lock);} else if (I = R. id. button4) {BTN. settag (ktextturnonkey, Res. getstring (R. string. combineflag0); BTN. settag (ktextturnoffkey, Res. getstring (R. string. notcombineflag0);} else if (I = R. id. button5) {BTN. settag (ktextturnonkey, Res. getstring (R. string. combineflag1); BTN. settag (ktextturnoffkey, Res. getstring (R. string. notcombineflag1);} BTN. settext (string) BTN. gettag (ktextturnonkey); BTN. setonclicklistener (this);} onclicklistener listener = new onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubif (v. GETID () = R. id. button6) {PM. gotosleep (syst Emclock. uptimemillis ();} else if (v. GETID () = R. id. button7) {PM. reboot ("robin test the reboot API");} else if (v. GETID () = R. id. button8) {message msgsend = handler. obtainmessage (kmeanuseractivity, 10, 0); handler. sendmessagedelayed (msgsend, 35*1000) ;}}; BTN = (button) findviewbyid (R. id. button6); BTN. setonclicklistener (listener); BTN = (button) findviewbyid (R. id. button7); BTN. setonclicklistener (listener); BTN = (Button) findviewbyid (R. id. button8); BTN. setonclicklistener (listener); PM = (powermanager) getsystemservice (context. power_service) ;}int mflag = 0; long time = 0; Public void onclick (view v) {time = system. currenttimemillis (); // todo auto-generated method stubtextview TV = (textview) V; int id = TV. GETID (); If (ID = R. id. button4) {If (v. gettag (kstatekey ). equals (kstateoff) {mflag | = powermanager. acquire_causes_wakeu P;} else {mflag = mflag &~ Powermanager. acquire_causes_wakeup;} else if (ID = R. id. button5) {If (v. gettag (kstatekey ). equals (kstateoff) {mflag | = powermanager. on_after_release;} else {mflag = mflag &~ Powermanager. on_after_release;} else {If (v. gettag (kstatekey ). equals (kstateoff) {unreleaseotherlock (ID); int flag = 0; If (ID = R. id. button0) {flag = powermanager. partial_wake_lock;} else if (ID = R. id. button1) {flag = powermanager. screen_dim_wake_lock;} else if (ID = R. id. button2) {flag = powermanager. screen_bright_wake_lock;} else if (ID = R. id. button3) {flag = powermanager. full_wake_lock;} If (ID! = R. id. button0) {flag = Flag | mflag;} If (mflag = 0 | id = R. id. button0) {WL = PM. newwakelock (flag, tag); WL. acquire ();} else {int delaymillis = 1; if (mflag & powermanager. acquire_causes_wakeup )! = 0) delaymillis = 50*1000; message MSG = handler. obtainmessage (kmeanacquirelock, flag, 0); handler. sendmessagedelayed (MSG, delaymillis); If (mflag & powermanager. on_after_release )! = 0) {MSG = handler. obtainmessage (kcmdreleaselock, flag, 0); delaymillis = delaymillis + 20*1000; handler. sendmessagedelayed (MSG, delaymillis) ;}} else if (WL! = NULL) {WL. release (); WL = NULL ;}} if (v. gettag (kstatekey ). equals (kstateoff) {TV. settag (kstatekey, kstateon); TV. settext (string) TV. gettag (ktextturnoffkey); log. I (TAG, "set to turn off in onclick");} else {TV. settag (kstatekey, kstateoff); TV. settext (string) TV. gettag (ktextturnonkey); log. I (TAG, "set to turn on in onclick") ;}} void unreleaseotherlock (INT ID) {handler. removemessages (kmeanacquirelock); handler. Removemessages (kcmdreleaselock); If (WL! = NULL) {WL. release (); WL = NULL;} button BTN = NULL; For (INT I = R. id. button0; I <= R. id. button3; I ++) {If (ID = I) continue; BTN = (button) findviewbyid (I); BTN. settag (kstatekey, kstateoff); BTN. settext (string) BTN. gettag (ktextturnonkey); log. I (TAG, "set to" + Btn. gettext () ;}} public void onresume () {time = system. currenttimemillis (); log. I (TAG, "onresume"); super. onresume ();} public void onpause () {long T = (System. currenttimemillis ()-Time)/1000; log. I (TAG, "pause after user activity time:" + T); super. onpause ();} public void ondestroy () {If (WL! = NULL) {WL. release (); WL = NULL;} super. ondestroy ();} final static int k1_acquirelock = 0; Final Static int kcmdreleaselock = 1; Final Static int k1_useractivity = 2; Class myhandler extends handler {public void handlemessage (Message MSG) {Switch (MSG. what) {Case kmeanacquirelock: int flag = MSG. arg1; WL = PM. newwakelock (flag, tag); WL. acquire (); break; Case kcmdreleaselock: If (WL! = NULL) {WL. release (); WL = NULL;} break; Case k1_useractivity: PM. useractivity (systemclock. uptimemillis (), true); int loopcnt = MSG. arg1; If (loopcnt --> 0) {message msgsend = handler. obtainmessage (kmeanuseractivity, loopcnt, 0); handler. sendmessagedelayed (msgsend, 20*1000);} break ;}}}}

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.