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
  • 49,491
  • 11
  • 98
  • 86
Koin Arab
  • 1,045
  • 3
  • 19
  • 35

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
  • 135,866
  • 28
  • 264
  • 277