Reputation: 1171
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?
Upvotes: 10
Views: 3679
Reputation: 507
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)
Upvotes: 5
Reputation: 9870
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
Upvotes: 3
Reputation: 9870
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?
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.
Upvotes: 0