简体   繁体   中英

Detect last widget being deleted

I created a widget which uses a background service for update notifications.

I want to stop the background service if the last widget is removed from the home screen(s).

How can I detect number of widgets remaining in home screen?

Ok, I figured out a way that doesn't involve having to store any persistent information. However even this looks like a hack to me. Inserted the following in the onReceive -

@Override
public void onReceive(Context context, Intent intent) {
        if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(sIntentAction)) {
            AppWidgetManager amgr = AppWidgetManager.getInstance(context);
            int length = amgr.getAppWidgetIds(intent.getComponent()).length;
            if (length==0) WidgetService.StopService(context);
        }
        super.onReceive(context, intent);
}

It works, and is the best solution I could find, but I am not very happy with it.

I needed to do this in onReceive and not onDeleted , since the intent object is required to do intent.getComponent() . I don't like the fact because the documentation claims that one will rarely need to touch the onReceive , and need to count remaining widgets seems to be a reasonable expectation in onDeleted . However unfortunately it provides count of widgets being deleted, and not count remaining. Is there any way to do this in the onDeleted instead?

(Slowly I seem to get the feeling that a lot of Android coding is actually hacking - and the code to get a job done mostly uses undocumented or unexpected avenues.)

If you created a AppWidgetProvider for your Widget , it should have an onUpdate() method. One of the arguments for onUpdate() is the int[] ids . This is actually an array of all of the AppWidget instances receiving onUpdate() requests. If the size of ids is 1, then the next delete is your last widget. :)

Hope this helps, FuzzicalLogic

I had a similar issue with my application and this is what I ended up doing to figure out if I had deleted the last widget or not, which allowed me to stop my service and update a preference item that was saved.

@Override
public void onDisabled(Context context)
{
    // When this get called I need to figure out how to set the preference 
    // homeWidgetSetup = false.
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    ComponentName thisAppWidget = new ComponentName(context, BasicHomeWidget.class.getName());
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);

    if(appWidgetIds.length < 1)
    {
        Log.d(TAG, "Removed the last widget!");
        Intent intent = new Intent(context, KMMDService.class);
        intent.putExtra("lastWidgetDeleted", true);
        context.startService(intent);
    }
}

Then I just tested looked at the extra passed to my service on onStartCommand and if has "lastWidgetDeleted" defined as true then I would stop the service and update my preferences.

may be answer is too late but onEnabled and onDisabled is called only when first widget is instantiated and when last widget is removed for a particular appwidget provider. Hence the above hacky and other technecs are not required.

if still you have doubts please go through the Android doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM