检测最后一个被删除的小部件

发布于 2024-12-24 18:21:22 字数 91 浏览 3 评论 0原文

我创建了一个使用后台服务进行更新通知的小部件。

如果从主屏幕中删除最后一个小部件,我想停止后台服务。

如何检测主屏幕中剩余的小部件数量?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ヅ她的身影、若隐若现 2024-12-31 18:21:22

好吧,我找到了一种不需要存储任何持久信息的方法。然而,即使这对我来说也像是一个黑客。在 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);
}

它有效,并且是我能找到的最佳解决方案,但我对此不太满意。

我需要在 onReceive 而不是 onDeleted 中执行此操作,因为 intent 对象需要执行 intent.getComponent().我不喜欢这一事实,因为文档声称人们很少需要接触 onReceive,并且需要计算剩余的小部件似乎是 onDeleted 中的合理期望。然而不幸的是,它提供了被删除的小部件的计数,而不是剩余的计数。有什么方法可以在 onDeleted 中执行此操作吗?

(慢慢地,我似乎感觉到很多 Android 编码实际上是黑客行为 - 并且完成工作的代码大多使用无证或意想不到的途径。)

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.)

夜访吸血鬼 2024-12-31 18:21:22

如果您为 Widget 创建了一个 AppWidgetProvider,它应该有一个 onUpdate() 方法。 onUpdate() 的参数之一是 int[] ids。这实际上是接收 onUpdate() 请求的所有 AppWidget 实例的数组。如果 ids 的大小为 1,那么下一个删除就是您的最后一个小部件。 :)

希望这有帮助,
模糊逻辑

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

峩卟喜欢 2024-12-31 18:21:22

我的应用程序也遇到了类似的问题,这就是我最终要做的事情,以确定我是否删除了最后一个小部件,这使我能够停止服务并更新已保存的首选项。

@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);
    }
}

然后我只是测试了一下在 onStartCommand 上传递给我的服务的额外内容,如果“lastWidgetDeleted”定义为 true,那么我将停止服务并更新我的首选项。

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.

热鲨 2024-12-31 18:21:22

可能答案为时已晚,但仅当实例化第一个小部件以及为特定 appwidget 提供程序删除最后一个小部件时,才会调用 onEnabled 和 onDisabled 。因此,不需要上述 hacky 和其他技术。

如果您仍然有疑问,请浏览 Android文档

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文