0

I have been trying to listen to Battery BroadCast events. Plugged/Unplugged.

public class BatteryReceiver extends BroadcastReceiver {
    public BatteryReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

        if(status == BatteryManager.BATTERY_STATUS_CHARGING){
            Toast.makeText(context, "Charging", Toast.LENGTH_SHORT).show();
        } else if(status == BatteryManager.BATTERY_STATUS_DISCHARGING || status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
            Toast.makeText(context, "Not charging", Toast.LENGTH_SHORT).show();
        }
    }
}

I have added the manifest actions :

<receiver
            android:name=".BatteryReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
            </intent-filter>
 </receiver>

1 Answers1

0

If you want to track when it is plugged/unplugged only, I think you can check the Intent received.

public class BatteryReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent != null) {
            String action =  intent.getAction();
            if(action != null) {
                if(action.equals("android.intent.action.ACTION_POWER_CONNECTED"){
                    Toast.makeText(context, "Charging", Toast.LENGTH_SHORT).show();
                } else if(action.equals("android.intent.action.ACTION_POWER_DISCONNECTED"){
                    Toast.makeText(context, "Not charging", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

UPDATE

But if you really want to check if Battery is Charging, you must change your code.

First, I notice that when you register your BroadcastReceiver via AndroidManifest.xml, intent received does not have any extra (in log, it would appear hasExtras when you print the intent:

Intent { act=android.intent.action.ACTION_POWER_DISCONNECTED flg=0x4000010 cmp=com.pivoto.myapplication/.BootCompletedReceiver }
Intent { act=android.intent.action.ACTION_POWER_CONNECTED flg=0x4000010 cmp=com.pivoto.myapplication/.BootCompletedReceiver }

Then, I searched in StackOverflow and I found this ANSWER (please, check it for more details):

So, is there a way to "request" the battery status when you want:

public class BootCompletedReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent != null) {
            Intent oneTimeOnlyBatteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
            int status = -1;
            if(oneTimeOnlyBatteryIntent != null)
                status = oneTimeOnlyBatteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            if(status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL) {
                Toast.makeText(context, "Charging", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Not charging", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
Community
  • 1
  • 1
guipivoto
  • 18,327
  • 9
  • 60
  • 75