10

colleagues! My problem is: I`ve got an App Widget with Configuration Activity(min SDK - 2.1), it works properly but sometimes it begins working more slowly. I logged and found out that before updating my App Widget method onUpdate received an array of App Widget Ids

  @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        Log.d("onUpdate", Arrays.toString(appWidgetIds));

But the truth was that I had only two instances of App Widget on Home Screen but the lenth of array is more then 2 (two): 4, sometimes 5 or 6. How can it be so? The reason of slowness of my AppWidget Updates is that I have to call onUpdate-method for all ids but some ids (as one said "phantom widgets") are not real and correct.

Maybe, someone have encountered with such problem and would help me to figure out how to handle only real app widget ids.

P.S. Uninstalling and then reinstalling app widget (and after that all the ghost widgets were gone) - are not the proper solving of my problem. I`d like to controll ghost and real widget programmatically.

Has someone any idea how to fix this bug?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Alex Zezekalo
  • 1,031
  • 2
  • 17
  • 34
  • This is a pretty common situation in android unfortunately. AFAIK you get a phantom widget if the user: **1)** Places a widget with a config activity, and backs out of the activity before completing it, or **2)**, attempts to place a widget on a screen that does not have space for it. I'm not aware of any mechanism to programmatically detect if a widget is a 'phantom' or not once it has been created. – Tim Nov 20 '12 at 20:42
  • Thank you for your comment... It`s a pity... – Alex Zezekalo Nov 21 '12 at 09:09

3 Answers3

4

This code will delete the widgets (all, even the ones on screen), so is not the ideal solution

ComponentName cn = new ComponentName(getApplicationContext(), YourWidget.class);
AppWidgetManager appWidgetManager =  AppWidgetManager.getInstance(getApplicationContext());
int[] appWidgetIds1 = appWidgetManager.getAppWidgetIds(cn);
AppWidgetHost host = new AppWidgetHost(getApplicationContext(), 0);
for (int appWidgetId : appWidgetIds1) {
    host.deleteAppWidgetId(appWidgetId);
}

Still having problems with deletion of Lock Screen widgets on 4.2 (onReceive never called)

BeNeXuS
  • 497
  • 5
  • 11
3

Ok, I searched a little bit on the web and find out, that this seems to be an allover android bug which is not fixed now. If you are able to, please try to clear the cache. Go to settings-->applications-->manage applications. Then go to your home-launcher application and clear cache. Then please start your app again and look if there are still old app-widgets. I found some interesting on the web about this:

http://forum.xda-developers.com/showthread.php?t=1030804

http://eagle.phys.utk.edu/guidry/android/appWidget.html

Even this is discussed on Android developers page and the Group-Post:

http://developer.android.com/guide/topics/appwidgets/index.html#AppWidgetProvider

https://groups.google.com/forum/?fromgroups=#!msg/android-developers/Nl0e06rDCRY/4nAh3xnKBeQJ

They talk about a problem in 1.5, but I think that is an problem even with higher systems. In the Group post there is an example how to fix this:

      @Override
        public void onReceive(Context context, Intent intent) {
         final String action = intent.getAction();
         if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
         final int appWidgetId = extras.getInt
       (AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
       if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
        this.onDeleted(context, new int[] { appWidgetId });
          }
       } else {
          super.onReceive(context, intent);
       }
      } 

It seems that not every app-widget is automatically deleted, even if the user deletes the app or replace the widget from the home-screen. Please give me some feedback if this is the right direction. If yes, we need another approach for this problem. For me it is also interesting, but at the moment I got no laptop here to check it. Thanks

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • By now I could figure out such points: 1) for home screen - this bug persistently was reproduced in case of back out of Configuration Activity by using home button but wasn`t reproduced when I attempted to place a widget on a screen that does not have space for it (HTC Nexus One 2.1). Clearing cash isn`t possible because cache = 0 in my case. 2)for lock screen (android 4.2) - this bug was reproduced persistently when I was trying to delete instance of appwidget from lock screen. In such case it never called onReceive and onDelete methods -strange behaviour for appWidgetProvider, isn`t it? – Alex Zezekalo Nov 21 '12 at 14:02
  • Example with solving problem 1.5 doesn`t fit to me because 1) for home screen - as I investigated bug appears while adding widget to home screen, not removing. 2) for lock screen - indeed, it appears while deleting widget from lock screen, but in that case method onReceive isn`t called. – Alex Zezekalo Nov 21 '12 at 14:21
  • very strange. I´m gonna give it a try when I am at home. It must be possible to fix this problem in any way..I hope.... – Opiatefuchs Nov 21 '12 at 15:13
  • So do I... I`ll be much obliged for your help. – Alex Zezekalo Nov 21 '12 at 15:21
  • So i tried some stuff, downloaded an widget tutorial (was too lazy to do it by myself) and testet it on my device Samsung Galaxy S3 with jelly bean 4.1.1 . The appWidgetId length is always 1, so no old widgets are returned. But the Widget ID increases every time I remove the app widget from homescreen and move it back to. After restart my device, the Widget Id is at it´s old value. So, could you please give me some information, which Android verison you are using, and at which version you are developing this app? Thanks... – Opiatefuchs Nov 22 '12 at 06:57
  • I have tested on device HTC Nexus One (SDK 2.1), Asus Nexus 7 (SDK 4.2)... Indeed, increasing widget id is normal for such situation you described, but I mean increasing lenth of appwidgetids array. If you want to reproduce such kind of bug try doing as I said before 1) for home screen - this bug persistently was reproduced in case of back out of Configuration Activity by using home button 2) for lock screen - you should have a tablet (Nexus 7 or 10) with Android 4.2 In early version of SDK there aren`t lock screen widgets. By now I think that it`s a bug of new SDK. – Alex Zezekalo Nov 22 '12 at 09:11
  • I found some intresting and helpful information at this link [link] (http://malubu.wordpress.com/2012/06/13/the-configuration-bug/) – Alex Zezekalo Nov 22 '12 at 10:34
  • I think too, sorry that I couldn´t help You. This link is very helpfull,I hope the android-engineers could fix this in the future and you will find another way without slowing your app down. For now, I couldn´t get a good solution. – Opiatefuchs Nov 22 '12 at 17:28
0

I had the same problem in an app that I wrote before some month, in my case it doesn´t matter. I found a tutorial how to delete "phantom-widgets". I´ve never tested it, just try it:

http://blog.fiziksphreak.com/2011/07/28/remove-phantom-widgets-in-android/

But, are you sure, that this is slowing down Your app? I can´t imagine that this would be expensive on resources?

EDIT

Instead of killing old apps, this could help:

    Bundle extras = intent.getExtras();
     int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);

or

    Bundle extras = intent.getExtras();
     int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);

I don´t know if this is what you search.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49