When the cell phone power changes, the system will send the Intent Action to android. intent. action. BATTERY_CHANGED constant broadcast; when the cell phone power is too low, the system will send Intent Action as android. intent. action. broadcast of the BATTERY_LOW constant.
When the battery status of the mobile phone is restored from insufficient power, the system will send a broadcast that the Intent Action is android. intent. action. BATTERY_OKAY constant.
The following is a simple example:
Aggreger:
Package com. home. explorer; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. OS. bundle; import android. widget. toast; public class BatteryReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {if (Intent. ACTION_BATTERY_OKAY.equals (intent. getAction () {Toast. makeText (context, "power restored, Available! ", Toast. LENGTH_LONG). show () ;}if (Intent. ACTION_BATTERY_LOW.equals (intent. getAction () {Toast. makeText (context," the power is too low. Please charge it as soon as possible! ", Toast. LENGTH_LONG ). show ();} if (Intent. ACTION_BATTERY_CHANGED.equals (intent. getAction () {Bundle bundle = intent. getExtras (); // get the current power int current = bundle. getInt ("level"); // get the total power int total = bundle. getInt ("scale"); StringBuffer sb = new StringBuffer (); sb. append ("current power:" + current * 100/total + "%" + ""); // if the current power is less than 15% of the total power, if (current * 1.0/total <0.15) {sb. append ("power is too low, please charge as soon as possible! ");} Else {sb. append (" enough power, please feel free to use it! ");} Toast. makeText (context, sb. toString (), Toast. LENGTH_LONG ). show () ;}} package com. home. explorer; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. OS. bundle; import android. widget. toast; public class BatteryReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {if (Intent. ACTION_ B ATTERY_OKAY.equals (intent. getAction () {Toast. makeText (context, "the power has been restored and can be used! ", Toast. LENGTH_LONG). show () ;}if (Intent. ACTION_BATTERY_LOW.equals (intent. getAction () {Toast. makeText (context," the power is too low. Please charge it as soon as possible! ", Toast. LENGTH_LONG ). show ();} if (Intent. ACTION_BATTERY_CHANGED.equals (intent. getAction () {Bundle bundle = intent. getExtras (); // get the current power int current = bundle. getInt ("level"); // get the total power int total = bundle. getInt ("scale"); StringBuffer sb = new StringBuffer (); sb. append ("current power:" + current * 100/total + "%" + ""); // if the current power is less than 15% of the total power, if (current * 1.0/total <0.15) {sb. append ("power is too low, please charge as soon as possible! ");} Else {sb. append (" enough power, please feel free to use it! ") ;}Toast. makeText (context, sb. toString (), Toast. LENGTH_LONG). show ();}}}
Configure aggreger:
receiver android:name="com.home.receiver.BatteryReceiver"> <intent-filter> <action android:name="android.intent.action.BATTERY_CHANGED" /> <action android:name="android.intent.action.BATTERY_OKAY"/> <action android:name="android.intent.action.BATTERY_LOW"/> </intent-filter> </receiver> <receiver android:name="com.home.receiver.BatteryReceiver"> <intent-filter> <action android:name="android.intent.action.BATTERY_CHANGED" /> <action android:name="android.intent.action.BATTERY_OKAY"/> <action android:name="android.intent.action.BATTERY_LOW"/> </intent-filter> </receiver>