Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. UnRegister BroadcastReceiver Dynamically While Exiting App throws IllegalArgumentException

UnRegister BroadcastReceiver Dynamically While Exiting App throws IllegalArgumentException

Scheduled Pinned Locked Moved Android
question
2 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    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")
    
    Richard DeemingR 1 Reply Last reply
    0
    • D Django_Untaken

      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")
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups