Android Development using broadcastreceiver real-time monitoring of power (source code sharing)

Source: Internet
Author: User

Real-time monitoring of the phone's battery power and boot-up capability in the Android system is achieved through the Broadcastreceiver component. We can dynamically register an instance of this class through the Context.registerreceiver () method or statically register through <Receiver> tag in androidmanifest. Xml. Note: If we register a receiver in the activity.onresume () implementation, we should unregister the activity in the activity life cycle of the OnPause method. (This will reduce unnecessary system overhead). Remember that you cannot unregister the activity at Onsaveinstancestate () because this method will not be started until the user returns to the history stack. Below we use the dynamic registration Broadcastreceiver to realize the power monitoring.

(1) Adding permissions in the manifest file

  <uses-permission android:name= "Android.permission.BATTERY_STATS"/>

(2) Register to cancel receiver during activity life cycle

@Overrideprotected void Onresume () {//TODO auto-generated method Stubsuper.onresume (); reciver=new Batterybroadcastreciver ();//Create a filter intentfilter intentfilter=new intentfilter (intent.action_battery_changed); Registerreceiver (Reciver, intentfilter);} @Overrideprotected void OnPause () {//TODO auto-generated method Stubsuper.onpause (); Unregisterreceiver (reciver);}

(3) Inherit Broadcastreceiver interface

public class Batterybroadcastreciver extends broadcastreceiver{

@Override
public void OnReceive (context context, Intent Intent) {
TODO auto-generated Method Stub
if (Intent.getaction (). Equals (intent.action_battery_changed)) {
Get the current power of the system
int Level=intent.getintextra ("level", 0);
Get Total system power
int Total=intent.getintextra ("scale", 100);
Textview.settext ("Current charge:" + (level*100)/total+ "%");
Triggered when the charge is less than 15%
if (level<15) {
Toast.maketext (Mainactivity.this, "current charge is less than 15%", Toast.length_long). Show ();
}

}
}

}

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.