Question

I am having an application where user can register and unregister receiver from Settings to enable and disable service of app.

I have taken a toggle button to let user control the app service.I registered a receiver on its on state and unregistered the same on its off state.

It works fine when I enable the app service by making toggle button in on state,it successfully registered the receiver and gives me toast.

But when I try to click again on toggle button making it in off state,it gives me exception on line unregisterReceiver(..).

Here is the code:

    ...
    onoff.setOnClickListener(new OnClickListener() 
    {               
        @Override
        public void onClick(View v)
        {   
            BroadcastReceiver b=new SmsReactor();
            if(onoff.isChecked())
            {
                try               
                {                               
                    IntentFilter iFilter=new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
                    iFilter.setPriority(100);
                    registerReceiver(b,iFilter);

                    Toast.makeText(context,"Service Started!",Toast.LENGTH_SHORT).show();
                }
                catch (Exception e) {

                    onoff.setChecked(false);
                    Toast.makeText(context,"Sorry,Couldn't start the service!Try again.",Toast.LENGTH_SHORT).show();
                    e.printStackTrace();                  
                }                   
          }
          else
          {
                try
                {                           
                    unregisterReceiver(b); // here it gives me exception                
                    Toast.makeText(context,"Service Stopped!",Toast.LENGTH_SHORT).show();
                }
                catch (Exception e){

                    onoff.setChecked(true);
                    Toast.makeText(context,"Sorry,Couldn't stop the service!Try again.",Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }           
          }
       }
   });
    ...

And the logcat:

12-07 10:16:37.113: W/System.err(445): java.lang.IllegalArgumentException: Receiver not registered: com.xxx.android.xxx.SmsReactor@44e1c500
12-07 10:16:37.113: W/System.err(445):  at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:667)
12-07 10:16:37.123: W/System.err(445):  at android.app.ApplicationContext.unregisterReceiver(ApplicationContext.java:747)
12-07 10:16:37.123: W/System.err(445):  at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:321)
12-07 10:16:37.123: W/System.err(445):  at com.xxx.android.xxx.Settings$2.onClick(Settings.java:90)
12-07 10:16:37.123: W/System.err(445):  at android.view.View.performClick(View.java:2364)
12-07 10:16:37.123: W/System.err(445):  at android.widget.CompoundButton.performClick(CompoundButton.java:98)
12-07 10:16:37.123: W/System.err(445):  at android.view.View.onTouchEvent(View.java:4179)
12-07 10:16:37.123: W/System.err(445):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
12-07 10:16:37.123: W/System.err(445):  at android.view.View.dispatchTouchEvent(View.java:3709)
12-07 10:16:37.123: W/System.err(445):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-07 10:16:37.123: W/System.err(445):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-07 10:16:37.123: W/System.err(445):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-07 10:16:37.123: W/System.err(445):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-07 10:16:37.123: W/System.err(445):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-07 10:16:37.123: W/System.err(445):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
12-07 10:16:37.123: W/System.err(445):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
12-07 10:16:37.123: W/System.err(445):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
12-07 10:16:37.123: W/System.err(445):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
12-07 10:16:37.123: W/System.err(445):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
12-07 10:16:37.123: W/System.err(445):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 10:16:37.133: W/System.err(445):  at android.os.Looper.loop(Looper.java:123)
12-07 10:16:37.133: W/System.err(445):  at android.app.ActivityThread.main(ActivityThread.java:4363)
12-07 10:16:37.133: W/System.err(445):  at java.lang.reflect.Method.invokeNative(Native Method)
12-07 10:16:37.133: W/System.err(445):  at java.lang.reflect.Method.invoke(Method.java:521)
12-07 10:16:37.133: W/System.err(445):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-07 10:16:37.133: W/System.err(445):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-07 10:16:37.133: W/System.err(445):  at dalvik.system.NativeStart.main(Native Method)

Please help me finding out where I am doing wrong?! Any help appreciated.

Thanks in advance!

Was it helpful?

Solution

Just define this BroadcastReceiver b=new SmsReactor(); outside of Button's click like, OnCreate() and try again let me know what happen,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top