1
我有一個應用程序,用戶可以在Settings中註冊和取消註冊接收器以啓用和禁用應用程序的服務。Android:在unregisterReceiver()上獲取java.lang.IllegalArgumentException()
我已經採取了切換按鈕讓該應用service.I登記在其上狀態接收機和註銷同一其關閉狀態的用戶控制。
當我通過在的狀態下開啓切換按鈕來啓用應用程序服務時,它正常工作,它成功註冊了接收器並給了我祝酒。
但是,當我嘗試再次點擊切換按鈕使其在關閉狀態時,它給我例外行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: [email protected]
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)
請幫我找出我在哪裏做錯了?任何幫助讚賞。
在此先感謝!
非常感謝......它的工作! :) +1 ...在5分鐘內接受答案,所以說我不能在那之前。並再次感謝! – Hiral
你明白髮生了什麼事嗎? – user370305
是的......每當我們點擊一個按鈕,它就需要廣播接收器的新實例。這是造成問題的原因! – Hiral