This article is an example of how Android gets the battery usage for mobile phones. Share to everyone for your reference. Specifically as follows:
Principle Overview:
The acquisition of cell phone battery power is also commonly used in the development of applications, the mobile phone battery in the Android system, the change in the message is through the Intent broadcast to achieve, commonly used Intent ACTION has Intent.action_battery_ CHANGED (when the battery charge changes), Intent.action_battery_low (when the battery is up to the lower limit), and Intent.action_battery_okay (when the battery charge is restored from low to high).
When you need to obtain information about battery power in your program, you need to register the Broadcastreceiver component for your application, and when a specific action event occurs, the system will broadcast, and the application can receive broadcasts via Broadcastreceiver. and to deal with accordingly.
Main.xml Layout file:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent" >
<togglebutton android:id= "
@+id/tb" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
android:texton= "stop Getting Power Information"
android:textoff= "get power Information"/>
< TextView android:id= "@+id/tv"
android:layout_width= "fill_parent" android:layout_height= "Wrap_content"
/>
</LinearLayout>
Batteryactivity class:
Package com.ljq.activity;
Import android.app.Activity;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.os.Bundle;
Import Android.widget.CompoundButton;
Import Android.widget.TextView;
Import Android.widget.ToggleButton;
Import Android.widget.CompoundButton.OnCheckedChangeListener;
public class Batteryactivity extends activity {private ToggleButton tb=null;
Private TextView Tv=null;
Private Batteryreceiver Receiver=null;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Receiver=new Batteryreceiver ();
tv= (TextView) Findviewbyid (r.id.tv);
tb= (ToggleButton) Findviewbyid (R.ID.TB); Tb.setoncheckedchangelistener (New Oncheckedchangelistener () {public void oncheckedchanged (Compoundbutton compoundBu
Tton, Boolean ischecked) {//Get battery charge if (ischecked) { Intentfilter filter=new Intentfilter (intent.action_battery_changed); Registerreceiver (receiver, filter)//register Broadcastreceiver}else {//stop getting battery power unregisterreceiver
(receiver);
Tv.settext (NULL);
}
}
}); Private class Batteryreceiver extends broadcastreceiver{@Override public void onreceive (context context, inte NT Intent) {int Current=intent.getextras (). GetInt ("level");//get current charge int Total=intent.getextras (). GetInt ("scal
E ");//Get Total electrical energy int percent=current*100/total; Tv.settext ("The current electricity is" +percent+ "%.)
");
}
}
}
Run Result:
I hope this article will help you with your Android program.