1

This is my code:

public class PowerConnectionReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        Bundle extras = intent.getExtras();

        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);

        intent = new Intent (context, MainActivity.class);
        intent.putExtra("Charging", isCharging);
        startActivity(intent);
    }
}

I think it should work properly but I get an error at startActivity(). It looks like it was undefinded. Does anybody know what's wrong with this?

Xaver Kapeller
  • 48,336
  • 11
  • 94
  • 85
Koin Arab
  • 987
  • 3
  • 16
  • 33

1 Answers1

5

BroadcastReceiver does not extend Context and so it does not have the method startActivity. You should use the context that is passed to onReceive for that:

context.startActivity(intent);
MByD
  • 133,244
  • 25
  • 260
  • 270