0

我正在制作小部件,它将由 Intent.ACTION_TIME_TICK 更新。

所以我注册了接收者(我的 AppWidgetProvider):

private IntentFilter intentFilter = new IntentFilter(Intent.ACTION_TIME_TICK);

@Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        context.getApplicationContext().registerReceiver(this, intentFilter);   
    }

一切正常。但是当我尝试注销接收者时,应用程序崩溃:

@Override
public void onDisabled(Context context) {   
    context.getApplicationContext().unregisterReceiver(this);  
    super.onDisabled(context);
}

它抛出

java.lang.IllegalArgumentException: Receiver not registered:
com.holoware.holoclock.HoloClockWidgetProvider@41a3a3e8

日志:

D/AndroidRuntime( 3937): Shutting down VM
W/dalvikvm( 3937): threadid=1: thread exiting with uncaught exception (group=0x4
19e8300)
E/AndroidRuntime( 3937): FATAL EXCEPTION: main
E/AndroidRuntime( 3937): java.lang.RuntimeException: Unable to start receiver co
m.holoware.holoclock.HoloClockWidgetProvider: java.lang.IllegalArgumentException
: Receiver not registered: com.holoware.holoclock.HoloClockWidgetProvider@41d616
20
E/AndroidRuntime( 3937):        at android.app.ActivityThread.handleReceiver(Act
ivityThread.java:2362)
E/AndroidRuntime( 3937):        at android.app.ActivityThread.access$1500(Activi
tyThread.java:142)
E/AndroidRuntime( 3937):        at android.app.ActivityThread$H.handleMessage(Ac
tivityThread.java:1284)
E/AndroidRuntime( 3937):        at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime( 3937):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 3937):        at android.app.ActivityThread.main(ActivityThrea
d.java:4931)
E/AndroidRuntime( 3937):        at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime( 3937):        at java.lang.reflect.Method.invoke(Method.java:5
11)
E/AndroidRuntime( 3937):        at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 3937):        at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:558)
E/AndroidRuntime( 3937):        at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime( 3937): Caused by: java.lang.IllegalArgumentException: Receiver
 not registered: com.holoware.holoclock.HoloClockWidgetProvider@41d61620
E/AndroidRuntime( 3937):        at android.app.LoadedApk.forgetReceiverDispatche
r(LoadedApk.java:654)
E/AndroidRuntime( 3937):        at android.app.ContextImpl.unregisterReceiver(Co
ntextImpl.java:1166)
E/AndroidRuntime( 3937):        at android.content.ContextWrapper.unregisterRece
iver(ContextWrapper.java:378)
E/AndroidRuntime( 3937):        at com.holoware.holoclock.HoloClockWidgetProvide
r.onDisabled(HoloClockWidgetProvider.java:53)
E/AndroidRuntime( 3937):        at android.appwidget.AppWidgetProvider.onReceive
(AppWidgetProvider.java:91)
E/AndroidRuntime( 3937):        at com.holoware.holoclock.HoloClockWidgetProvide
r.onReceive(HoloClockWidgetProvider.java:63)
E/AndroidRuntime( 3937):        at android.app.ActivityThread.handleReceiver(Act
ivityThread.java:2355)
E/AndroidRuntime( 3937):        ... 10 more
4

2 回答 2

0

确保您onDisabled()没有被呼叫两次还检查您是否正在以其他方法取消注册接收器...

尝试将取消注册代码放入 try catch 中,如下面的代码..

@Override
    public void onDisabled(Context context) {       
        try{
                context.getApplicationContext().unregisterReceiver(this);
         } catch(Exception e){}
        super.onDisabled(context);
    }
于 2012-10-31T18:43:36.583 回答
0

问题是,onEnabled()仅在添加第一个小部件时才调用。如果小部件已经被添加并且您启动了您的应用程序,onEnabled()则不会被调用。这就是接收器未注册的原因,而您在取消注册时会遇到异常。我建议你Intent这样创建:

    Intent intent = new Intent(context, YourAppWidgetProvider.class);
    intent.setAction(action);
于 2012-11-02T14:33:34.403 回答