UnRegister BroadcastReceiver Dynamically While Exiting App throws IllegalArgumentException
-
Hello there. I have this BroadcastReciever which I register and unregister dynamically, in a BASE activity. The purpose, of this receiver, is very simple. I check if HOME button is pressed? The registration of the receiver is as follows: ActivityBase
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHomeWatcher = new HomeWatcher(this); mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() { @Override public void onHomePressed() { // my code goes here } @Override public void onHomeLongPressed() { // my code goes here } }); mHomeWatcher.startWatch();
}
protected void stopHomeWatcher(){ if(mHomeWatcher != null) { mHomeWatcher.stopWatch(); mHomeWatcher.setOnHomePressedListener(null); mHomeWatcher = null; } }
HomeWatcher
public class HomeWatcher {
public HomeWatcher(Context context) {
mContext = context;
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
}public void setOnHomePressedListener(OnHomePressedListener listener) { mListener = listener; mReceiver = new InnerReceiver(); } public void startWatch() { if (mReceiver != null) { mContext.registerReceiver(mReceiver, mFilter); } }
/* EXCEPTION IN THIS FUNCTION */
public void stopWatch() {
if (mReceiver != null) {
if(mContext != null)
mContext.unregisterReceiver(mReceiver);// <<==========THIS IS WHERE I GET EXCEPTION
}
}class InnerReceiver extends BroadcastReceiver { final String SYSTEM\_DIALOG\_REASON\_KEY = "reason"; final String SYSTEM\_DIALOG\_REASON\_GLOBAL\_ACTIONS = "globalactions"; final String SYSTEM\_DIALOG\_REASON\_RECENT\_APPS = "recentapps"; final String SYSTEM\_DIALOG\_REASON\_HOME\_KEY = "homekey"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION\_CLOSE\_SYSTEM\_DIALOGS)) { String reason = intent.getStringExtra(SYSTEM\_DIALOG\_REASON\_KEY); if (reason != null) { if(reason.trim().toLowerCase().equals("homekey")
-
Hello there. I have this BroadcastReciever which I register and unregister dynamically, in a BASE activity. The purpose, of this receiver, is very simple. I check if HOME button is pressed? The registration of the receiver is as follows: ActivityBase
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHomeWatcher = new HomeWatcher(this); mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() { @Override public void onHomePressed() { // my code goes here } @Override public void onHomeLongPressed() { // my code goes here } }); mHomeWatcher.startWatch();
}
protected void stopHomeWatcher(){ if(mHomeWatcher != null) { mHomeWatcher.stopWatch(); mHomeWatcher.setOnHomePressedListener(null); mHomeWatcher = null; } }
HomeWatcher
public class HomeWatcher {
public HomeWatcher(Context context) {
mContext = context;
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
}public void setOnHomePressedListener(OnHomePressedListener listener) { mListener = listener; mReceiver = new InnerReceiver(); } public void startWatch() { if (mReceiver != null) { mContext.registerReceiver(mReceiver, mFilter); } }
/* EXCEPTION IN THIS FUNCTION */
public void stopWatch() {
if (mReceiver != null) {
if(mContext != null)
mContext.unregisterReceiver(mReceiver);// <<==========THIS IS WHERE I GET EXCEPTION
}
}class InnerReceiver extends BroadcastReceiver { final String SYSTEM\_DIALOG\_REASON\_KEY = "reason"; final String SYSTEM\_DIALOG\_REASON\_GLOBAL\_ACTIONS = "globalactions"; final String SYSTEM\_DIALOG\_REASON\_RECENT\_APPS = "recentapps"; final String SYSTEM\_DIALOG\_REASON\_HOME\_KEY = "homekey"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION\_CLOSE\_SYSTEM\_DIALOGS)) { String reason = intent.getStringExtra(SYSTEM\_DIALOG\_REASON\_KEY); if (reason != null) { if(reason.trim().toLowerCase().equals("homekey")
Does the exception say that the receiver is not registered? At a guess, you're calling
unregisterReceiver
twice. I don't "do" Android dev, but I suspect you'd want something more like this:public void setOnHomePressedListener(OnHomePressedListener listener) {
mListener = listener;
}public void startWatch() {
if (mListener != null && mReceiver == null) {
mReceiver = new InnerReceiver();
mContext.registerReceiver(mReceiver, mFilter);
}
}public void stopWatch() {
if (mReceiver != null) {
mContext.unregisterReceiver(mReceiver);
mReceiver = null;
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer