Principle Overview:
The acquisition of cell phone battery power is also frequently used in application development. In Android systems, messages that change cell phone battery power are broadcast using Intent, and commonly used Intent actions include Intent. ACTION_BATTERY_CHANGED (when the battery power changes), Intent. ACTION_BATTERY_LOW (battery limit reached), and Intent. ACTION_BATTERY_OKAY (battery recovery from low to high ).
When you need to obtain battery information in the program, you need to register the BroadcastReceiver component for the application. When a specific Action event occurs, the system will broadcast it accordingly, applications can use BroadcastReceiver to receive broadcasts and process them 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 obtaining 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 batterypolicer extends ER = 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 compoundButton, boolean isChecked ){
// Obtain battery power
If (isChecked ){
IntentFilter filter = new IntentFilter (Intent. ACTION_BATTERY_CHANGED );
RegisterReceiver (receiver, filter); // register BroadcastReceiver
} Else {
// Stop obtaining battery power
UnregisterReceiver (receiver );
TV. setText (null );
}
}
});
}
Private class BatteryReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
Int current = intent. getExtras (). getInt ("level"); // obtain the current power
Int total = intent. getExtras (). getInt ("scale"); // get the total power
Int percent = current * 100/total;
TV. setText ("current power is" + percent + "%. ");
}
}
}
Running result