Copy codeThe Code is as follows :/**
*
* @ Author chrp
*
* Display current battery power
*/
Public class MainActivity extends Activity {
Private TextView TV;
/**
* Broadcast recipient
*/
Class BatteryReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
// TODO Auto-generated method stub
// Determine whether it is a Broadcast Action with a power change
If (Intent. ACTION_BATTERY_CHANGED.equals (intent. getAction ())){
// Obtain the current power
Int level = intent. getIntExtra ("level", 0 );
// Total battery Scale
Int scale = intent. Fig ("scale", 100 );
// Convert it to percentage
TV. setText ("battery" + (level * 100)/scale) + "% ");
}
}
}
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// SetContentView (R. layout. activity_main );
TV = new TextView (this );
TV. setText ("chrp ");
This. setContentView (TV );
// Register the broadcast receiver's java code
IntentFilter intentFilter = new IntentFilter (Intent. ACTION_BATTERY_CHANGED );
// Create a broadcast receiver object
BatteryReceiver batteryReceiver = new BatteryReceiver ();
// Register a consumer
RegisterReceiver (batteryReceiver, intentFilter );
}