我有一个服务,它获取广播并返回意图的唯一 ID:
PendingIntent pi;
public int Setup()
{
static AtomicInteger nextId = new AtomicInteger();
int Id = nextId.incrementAndGet();
Intent i = new Intent("INTENT_" + String.valueOf(Id));
pi = PendingIntent.getBroadcast(this, Id, i, PendingIntent.FLAG_UPDATE_CURRENT);
//rest of code goes here
return alarm.Id;
}
我从主活动中的单击事件调用该方法,我还注册了一个广播。(服务是绑定的和一切)。一切正常:
private Service service;
BroadcastReceiver br;
private void onClickSet(View v)
{
final int id = service.Setup();
br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
Toast.makeText(c, "text goes here", Toast.LENGTH_SHORT).show();
if (br != null)
{
Toast.makeText(c, "Unregistering previous receiver...", Toast.LENGTH_SHORT).show();
c.unregisterReceiver(br);
}
}
};
registerReceiver(br, new IntentFilter("INTENT_" + String.valueOf(id)));
}
正如我所说,一切正常,即使我重复点击 10 次,问题是如果我点击按钮太快,我会收到“快速点击多次按钮时接收广播错误”。堆栈跟踪如下:
09-24 09:46:25.756: D/AndroidRuntime(1382): Shutting down VM
09-24 09:46:25.756: W/dalvikvm(1382): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
09-24 09:46:25.854: E/AndroidRuntime(1382): FATAL EXCEPTION: main
09-24 09:46:25.854: E/AndroidRuntime(1382): java.lang.RuntimeException: Error receiving broadcast Intent { act=INTENT_2 flg=0x14 (has extras) } in com.example.project.Activity1$3@41753618
09-24 09:46:25.854: E/AndroidRuntime(1382): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:773)
09-24 09:46:25.854: E/AndroidRuntime(1382): at android.os.Handler.handleCallback(Handler.java:730)
09-24 09:46:25.854: E/AndroidRuntime(1382): at android.os.Handler.dispatchMessage(Handler.java:92)
09-24 09:46:25.854: E/AndroidRuntime(1382): at android.os.Looper.loop(Looper.java:137)
09-24 09:46:25.854: E/AndroidRuntime(1382): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-24 09:46:25.854: E/AndroidRuntime(1382): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 09:46:25.854: E/AndroidRuntime(1382): at java.lang.reflect.Method.invoke(Method.java:525)
09-24 09:46:25.854: E/AndroidRuntime(1382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-24 09:46:25.854: E/AndroidRuntime(1382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-24 09:46:25.854: E/AndroidRuntime(1382): at dalvik.system.NativeStart.main(Native Method)
09-24 09:46:25.854: E/AndroidRuntime(1382): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.example.project.Activity1$3@41788d60
09-24 09:46:25.854: E/AndroidRuntime(1382): at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:662)
09-24 09:46:25.854: E/AndroidRuntime(1382): at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1372)
09-24 09:46:25.854: E/AndroidRuntime(1382): at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:468)
09-24 09:46:25.854: E/AndroidRuntime(1382): at com.example.project.Activity1$3.onReceive(Activity1.java:131)
09-24 09:46:25.854: E/AndroidRuntime(1382): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:763)
09-24 09:46:25.854: E/AndroidRuntime(1382): ... 9 more
09-24 09:51:25.964: I/Process(1382): Sending signal. PID: 1382 SIG: 9