Broadcat Monitor Power changes

Source: Internet
Author: User

/*** Detect phone charge change * Use Registerreceiver (receiver, Intentfilter) to register the broadcast instead of registering in the manifest file <receiver><receiver> * required         Add Permission: * <uses-permission android:name= "Android.permission.BATTERY_STATS"/> * Register the broadcast in Onresume () * Registerreceiver (receiver, intentfilter); * Cancel the broadcast * UNREGISTERRECEIVER (receiver) in OnPause (); * Detection of processing power changes in the broadcast receiver * Detection intent charge change: intent.action_battery_changed * * get current charge * int level = Intent.getintextra ("level", 0 ); * Get total charge * int scale = Intent.getintextra (' scale ', 100); *   *     */ Public classMainactivityextendsActivity {TextView TV; Privatebroadcastreceiver Receiver; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (r.id.tv); } @Overrideprotected voidOnresume () {Super. Onresume (); Receiver=NewMyreceiver (); //Intent Filter, filter charge changeIntentfilter Intentfilter =NewIntentfilter (intent.action_battery_changed); //register a broadcast recipientRegisterreceiver (receiver, intentfilter); } @Overrideprotected voidOnPause () {Super. OnPause (); //unregister a broadcast recipientUnregisterreceiver (receiver); }    /*** Broadcast Recipient*/    Private classMyreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {if(Intent.getaction (). Equals (intent.action_battery_changed)) {//System's Power change ACTION//current charge "level"                intLevel = Intent.getintextra ("level", 0); //Total Power                intScale = Intent.getintextra ("scale", 100); //ShowTv.settext ("Current Charge" + (level *)/scale + "%"); }        }    }}

Broadcat Monitor Power changes

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.