In the manifest, you can add the following code.
<receiver android:name=".yourpackage.BroadCastNotifier" > <intent-filter> <action android:name="android.intent.action.BATTERY_LOW" /> </intent-filter> </receiver>
In BroadCastNotifier Class
package yourpackage; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class BroadCastNotifier extends BroadcastReceiver { private final static String TAG = "BroadCastNotifier"; @Override public void onReceive(Context context, Intent intent) { String intentAction = intent.getAction(); if(Intent.ACTION_BATTERY_LOW.equalsIgnoreCase(intentAction)){ Log.e(TAG, "GOT LOW BATTERY WARNING"); } } }
You will receive a GOT LOW BATTERY WARNING log message when your battery is low, also try this on your device.
Although the above code may work best, do not use BroadCast for such actions, you can control the battery level as described here . You can identify your battery with a code below which is easier.
int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = level / (float)scale;
source share