Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with appwidget appWidgetIds (real and phantom instances of widget)

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?

like image 710
Alex Zezekalo Avatar asked Nov 20 '12 10:11

Alex Zezekalo


1 Answers

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)

like image 82
BeNeXuS Avatar answered Oct 01 '22 23:10

BeNeXuS