I am working on an android project where I am registering a broadcast receiver in a class that extends BroadcastReceiver that receives intents for ACTION_POWER_CONNECTED, ACTION_POWER_DISCONNECTED and ACTION_SCREEN_OFF.
When these various intents are received it then calls functions within another class which extends Service.
When it receives the ACTION_POWER_CONNECTED it also calls a function called isPowerConnected that supposed to check whether the power is connected and if not it then enables the WakeLock.
The problem I am having is when I execute the isPowerConnected function it seems to work the first time but then every other time it receives:
android.content.ReceiverCallNotAllowedException: IntentReceiver
components are not allowed to register to receive intents.
I have no idea how I can fix this. Below is the code for the isPowerConnected.
public boolean isPowerConnected()
{
int pluggedIn = -1;
try
{
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
pluggedIn = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
}
catch (Exception ex)
{
Log.e("IsPowerConnected", ex.toString());
}
return pluggedIn == BatteryManager.BATTERY_PLUGGED_AC || pluggedIn == BatteryManager.BATTERY_PLUGGED_USB;
}
Thanks for any help you can provide.
You can’t register a new BroadcastReceiver in an existing BroadcastReceiver, which you are actually doing.