Search code examples
androidbroadcastreceiverbattery

BroadcastReceiver ACTION_POWER_CONNECTED


I've been trying to implement the simplest of things, each time you connect the device to a power resource, toast it.

for some reason each time I connect it, the application collapses. And because I need to unplug and plug again i cant see the logcat so I'm quite lost on why it's happening

My broadcastreceiver class

public class ChargingOnReceiver extends BroadcastReceiver {
        private Activity activity;
    public ChargingOnReceiver(Activity activity){
        this.activity = activity;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                            status == BatteryManager.BATTERY_STATUS_FULL;

        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

        if(isCharging){
            if(usbCharge){
                Toast.makeText(activity, "Your battery are now Charging Via USB" , Toast.LENGTH_SHORT).show();  
            }else{
                if(acCharge){
                    Toast.makeText(activity, "Your battery are now Charging Via AC Plug" , Toast.LENGTH_SHORT).show();
                }
            }
        }       
    }
  }

My mainActivity

private ChargingOnReceiver battery;
protected void onDestroy() {
        unregisterReceiver(battery);
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PreferenceManager.setDefaultValues(this,R.xml.search_settings_screen,false);
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        this.registerReceiver(battery, ifilter);
    }

My manifest

 <receiver android:name=".ChargingOnReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
        </intent-filter>
    </receiver>

I worked as it says here:

http://developer.android.com/training/monitoring-device-state/battery-monitoring.html


Solution

  • You do not need to pass activity object.
    onReceive() has the needed context use that and toast will appear

    @Override
    public void onReceive(Context context, Intent intent) {
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
                || status == BatteryManager.BATTERY_STATUS_FULL;
    
        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
    
        System.out.println("isCharging:" + isCharging);
    
        System.out.println("chargePlugType:" + chargePlug);
    
        if (isCharging) {
            if (usbCharge) {
                Toast.makeText(context, "Your battery are now Charging Via USB",
                        Toast.LENGTH_SHORT).show();
            } else {
                if (acCharge) {
                    Toast.makeText(context,
                            "Your battery are now Charging Via AC Plug",
                            Toast.LENGTH_SHORT).show();
                }
            }
        }
    }