2

我有一个应用程序,用户可以从设置中注册和取消注册接收器以启用和禁用应用程序服务。

我采用了一个切换按钮来让用户控制应用程序服务。我在其开启状态下注册了一个接收器,并在其关闭状态下取消注册它。

当我通过使切换按钮处于开启状态来启用应用程序服务时,它工作正常,它成功注册了接收器并给了我吐司。

但是,当我尝试再次单击切换按钮使其处于关闭状态时,它在 unregisterReceiver(..) 线上给了我异常。

这是代码:

    ...
    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();
                }           
          }
       }
   });
    ...

和日志猫:

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)

请帮我找出我做错了什么?!任何帮助表示赞赏。

提前致谢!

4

1 回答 1

1

只需BroadcastReceiver b=new SmsReactor(); 在按钮的点击之外定义这个,OnCreate()然后再试一次,让我知道发生了什么,

于 2011-12-07T05:09:52.720 回答