为什么在几乎所有教程或示例中人们都这样做:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
...
context.startService(resumeIntent);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
不是说每次执行update都要启动新的服务吗?这是最好的解决方案吗?还是发送广播更好? 而且不应该
context.startService(resumeIntent);
最好在 onEnabled 方法中完成?
最佳答案
不,服务只有在它尚未运行时才会启动:如果它已经在运行,它会在 onStartCommand
中发送一个新的 Intent 并且它可以适本地处理它。
就使用这个还是使用onEnabled
而言,它的优点是如果服务没有运行,每次都会重新启动服务:onEnabled
将启动服务,但如果服务因任何原因被终止,您可能无法为您重新启动它。
关于android - AppWidgetProvider(Widget) 与 Android 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7551612/