Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android Charge intent has no extra data

I am trying to display an alert in a specific Activity when the charging status changes. this is my receiver class:

public class BatteryChargeReceiver extends BroadcastReceiver {
private String TAG = "receiver";
private ChargeReceiverCallback callback;

public BatteryChargeReceiver(ChargeReceiverCallback callback) {

    this.callback =callback;
    Log.v(TAG,"Inside  Constructor (Callback)");
}

public interface ChargeReceiverCallback {
    public void onReceive(Intent intent);
}

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

}

}

And below is how I instantiate it from my MainActivity's onResume. MainActivity implements my interface and overrides onReceive(intent). I found out that at no stage does intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1) return anything else then -1.

BatteryChargeReceiver batteryReceiver = new BatteryChargeReceiver(this);
    IntentFilter filterPower = new IntentFilter("android.intent.action.ACTION_POWER_CONNECTED");
    registerReceiver(batteryReceiver, filterPower);
like image 535
Kepedizer Avatar asked Nov 29 '25 19:11

Kepedizer


1 Answers

Intent.ACTION_POWER_CONNECTED not delivering current plugged-in state - this is probably a change in how API works, but I could not locate any mention of such change in docs.

The broadcast Intent.ACTION_POWER_CONNECTED was never meant to receive current plugged-in type, but it merely lets the receiving end of power connection (ref.). In fact, BatteryService.java broadcasts plain intent without any extra values (lines 410~412 as of this writing):

Intent statusIntent = new Intent(Intent.ACTION_POWER_CONNECTED);
statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);

However, following code with

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="19" />

worked on Nexus 5:

public class MainActivity extends Activity {
    private static final String TAG = "PowerReceiver";

    private final BroadcastReceiver mPowerReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_POWER_CONNECTED.equals(action)) {
                Log.d(TAG, "Power connected");
                checkPluggedState(context, intent);
            } else if (Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
                Toast.makeText(context, "Power disconnected", Toast.LENGTH_SHORT).show();
            } else {
                Log.wtf(TAG, "Unknown action: " + action);
            }
        }
    };

    private void checkPluggedState(Context context, Intent intent) {
        Intent chargingIntent = registerReceiver(null, new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED));
        final int pluggedState = chargingIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        final String msg;
        switch (pluggedState) {
            case 0:
                msg = "The device is running on battery";
                break;
            case BatteryManager.BATTERY_PLUGGED_AC:
                msg = "Plugged into AC charger";
                break;
            case BatteryManager.BATTERY_PLUGGED_USB:
                msg = "Plugged into USB charger";
                break;
            case BatteryManager.BATTERY_PLUGGED_WIRELESS:
                msg = "Plugged into wireless charger";
                break;
            default:
                msg = "Unknown state: " + pluggedState;
        }

        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        registerReceiver(mPowerReceiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mPowerReceiver);
    }
}

Hope this helps.

like image 95
ozbek Avatar answered Dec 02 '25 08:12

ozbek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!