0

I'm making widget, that will be updated by Intent.ACTION_TIME_TICK.

So I registered reciever (my AppWidgetProvider):

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

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

Everything works fine. But when i try to unregister reciever, app crashes:

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

It throws

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

Logs:

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
Holoceo
  • 168
  • 2
  • 7
  • E/AndroidRuntime( 3974): FATAL EXCEPTION: main E/AndroidRuntime( 3974): java.lang.RuntimeException: Unable to start receiver co m.holoware.holoclock.HoloClockWidgetProvider: java.lang.IllegalArgumentException : Receiver not registered: com.holoware.holoclock.HoloClockWidgetProvider@41a3ae 70 – Holoceo Oct 31 '12 at 18:33
  • please use the edit option on the post and add you logs to your question with code formatting... – Praful Bhatnagar Oct 31 '12 at 18:39
  • can you provide some more logs.. just want to see if un-register is getting called twice... – Praful Bhatnagar Oct 31 '12 at 18:53

2 Answers2

0

make sure that your onDisabled() is not getting called twice also check if you are un-registering the the receiver at some other method also...

Try putting un-register code in try catch like following code..

@Override
    public void onDisabled(Context context) {       
        try{
                context.getApplicationContext().unregisterReceiver(this);
         } catch(Exception e){}
        super.onDisabled(context);
    }
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
0

The problem is, onEnabled() is called only when the first widget is added. If the widget has already been added and you start your application, onEnabled() won't be called. That's why the receiver is not registered and you get an exception unregistering it. I suggest you create your Intent this way:

    Intent intent = new Intent(context, YourAppWidgetProvider.class);
    intent.setAction(action);
Yulia Rogovaya
  • 924
  • 1
  • 8
  • 26
  • I registered reciever to the ApplicationContext, not to widget context. And `registerReceiver()` takes only IntentFilter as a parameter. – Holoceo Nov 02 '12 at 16:44
  • 1. Put your widget on your homescreen. 2. Restart the application. 3. Check if onEnabled() has been called. I suggest it's NOT called and that's why your receiver has not been registered. Then you remove the widget, onDisabled() gets called and you try to unregister the receiver which leads to an exception. – Yulia Rogovaya Nov 06 '12 at 10:10
  • I mean, if you create your intent the way I suggested, you won't need registerReceiver at all, the class name which you put in the constructor's parameter is enough to address your Provider. – Yulia Rogovaya Nov 06 '12 at 10:14