简体   繁体   English

检测最近删除的小部件

[英]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. 但是,即使对我来说,这似乎也是一个hack。 Inserted the following in the onReceive - 在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() . 我需要在onReceive而不是onDeleted执行此onDeleted ,因为intent对象需要执行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 . 我不喜欢这个事实,因为文档声称很少需要接触onReceive ,而需要计算剩余的小部件似乎是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? 有什么方法可以代替onDeleted吗?

(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.) (慢慢地,我似乎感觉到很多Android编码实际上是在被黑客入侵-而完成工作的代码大多使用未记录或意外的途径。)

If you created a AppWidgetProvider for your Widget , it should have an onUpdate() method. 如果您为Widget创建了一个AppWidgetProvider ,则它应具有onUpdate()方法。 One of the arguments for onUpdate() is the int[] ids . onUpdate()的参数之一是int[] ids This is actually an array of all of the AppWidget instances receiving onUpdate() requests. 实际上,这是所有接收onUpdate()请求的AppWidget实例的数组。 If the size of ids is 1, then the next delete is your last widget. 如果ids的大小为1,则下一个删除是您的最后一个小部件。 :) :)

Hope this helps, FuzzicalLogic 希望对您有所帮助,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. 然后,我只是测试了onStartCommand我的服务的额外onStartCommand ,如果将“ lastWidgetDeleted”定义为true,那么我将停止服务并更新我的首选项。

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. 可能答案太迟了,但是仅当实例化第一个小部件并且为特定的appwidget提供者删除了最后一个小部件时,才调用onEnabled和onDisabled。 Hence the above hacky and other technecs are not required. 因此,不需要上述技巧和其他技术。

if still you have doubts please go through the Android doc 如果您仍然有疑问,请阅读Android文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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