Broadcast Receiver Not Unregistering
I want to give the user the ability to unregister/register the broadcast receiver with the click of the button. When the button is pressed for the first time, the broadcast receive
Solution 1:
Your mReceiver value will always be equal to 1 because of these lines:
int mReceiver = 0;
mReceiver++;
I assume that mReceiver is an instance variable, in which case it should just be:
mReceiver++;
Better still, create a boolean value called isRegistered.
@OverridepublicvoidonClick(View v) {
if (!isRegistered) {
IntentFilterfilter=newIntentFilter(
Intent.ACTION_BATTERY_CHANGED);
registerReceiver(receiver, filter);
isRegistered = true;
}
else {
unregisterReceiver(receiver);
isRegistered = false;
}
}
Post a Comment for "Broadcast Receiver Not Unregistering"