AppWidget更新服务上的Widget不能阻止主机被删除主机、AppWidget、Widget

2023-09-05 01:25:55 作者:夜、放肆了寂寞

一旦AppWidget是从用户的Andr​​oid手机的主屏幕中删除我所要做的是,我要停止更新AppWidget的后台服务。

What I am trying to do is once the AppWidget is removed from the homescreen of the user's Android phone, I want to stop the background service that Updates the AppWidget.

下面是我使用的... code不明白什么是错?

Here is the code that I am using...don't understand whats wrong?

@Override
 public void onDeleted(Context context, int[] appWidgetIds){
 Intent serviceIntent = new Intent(context, UpdateService.class);
 context.stopService(serviceIntent);
 super.onDeleted(context, appWidgetIds);
}

任何想法?感谢您的帮助。

Any ideas? Thanks for the help.

推荐答案

很多研究,我终于固定它之后。我的目标是要停止后台服务,如果所有AppWidget实例从屏幕中删除。

After a lot of Research I finally fixed it. My goal here was to stop the background service if all AppWidget instances are removed from the screen.

这是什么做的......

This is what did it...

用于共享preferences切换1和0分享$ P $粉煤切换到1时,小部件的第一个实例是把屏幕上的用户。它切换回0时,应用程序插件的最后一个实例从屏幕上消失。

Used shared preferences to toggle 1 and 0. SharePref toggled to 1 when the very first instance of the widget is put on the screen by the user. It is toggled back to 0 when the last instance of the App widget is removed from the screen.

@Override的onReceive方法。侦听这些广播 - AppWidget启用(广播时,小部件的第一个实例是把屏幕上),AppWidget禁用(当控件的最后一个实例从屏幕上消失操作系统播出) 当AppWidget残疾人广播调用覆盖的方法OnDisabled方法。

@Override OnReceive method in the Service. Listen for these broadcasts - AppWidget Enabled (broadcast when the very first instance of the widget is put on the screen) , AppWidget Disabled (broadcast by the OS when the very last instance of the widget is removed from the screen) When AppWidget Disabled is broadcast call the overridden method OnDisabled method.

3.In @覆盖OnDisabled方法调用stopService。运行完美。

3.In @Override OnDisabled method call stopService. Works perfect.

记住OnDeleted和OnDisabled之间的差异。 OnDeleted当插件的实例被去除被调用,它并不意味着该插件被删除是最后一个。我希望我的服务仍然运行即使是在屏幕上一个Widget实例。

Remember the difference between OnDeleted and OnDisabled. OnDeleted is called when an instance of the widget is removed, it doesn't mean that the widget being deleted is the last one. I wanted my service to still run even if there was a single widget instance on the screen.

*的的另外,如果你不想做上述所有的,让Android的照顾服务等...使用IntentService。 * 的*

*Also if you do not want to do all of the above and let Android take care of the Service etc...use IntentService. **