我有一个应用程序,用户可以在“设置”中注册和取消注册接收器,以启用和禁用应用程序服务。
我已经使用切换按钮让用户控制应用服务。我在其开启状态下注册了一个接收器,并在其关闭<上取消注册/ strong>州。
当我通过在开启状态下切换按钮启用应用服务时,它工作正常,它已成功注册接收器并给我祝酒。
但是当我尝试再次点击切换按钮使其处于关闭状态时,它会在行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();
}
}
}
});
...
和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)
请帮我找出我做错的地方?!任何帮助表示赞赏。
提前致谢!
答案 0 :(得分:1)
只需定义此BroadcastReceiver b=new SmsReactor();
在Button的点击之外,像OnCreate()
,然后再试一次让我知道发生了什么,