简体   繁体   English

我如何使用后台服务Android馈送小部件

[英]How to I feed Widget with Background Service Android

I have the widget property in my application. 我的应用程序中有widget属性。 I show the data from the location on the widget but do not show the current data on the layout after 3 4 hours. 我在窗口小部件上显示该位置的数据,但在3 4小时后未在布局上显示当前数据。 I can add it to the homepage without problems, but it stops after 3 or 4 hours. 我可以毫无问题地将其添加到首页,但是3或4个小时后它将停止。 Also, after I turn the phone off and on, the widget layout looks empty. 另外,在我关闭手机电源后,小部件的布局看起来是空的。 How do I feed a widget with a service? 如何为小部件提供服务?

Just make logic of updating appwidget in IntentService (you can schedule it with AlarmManager or JobScheduler) and in appwidget-provider set android:updatePeriodMillis="0". 只需在IntentService(您可以使用AlarmManager或JobScheduler对其进行调度)和appwidget-provider中设置android:updatePeriodMillis =“ 0”的情况下进行更新appwidget的逻辑即可。 To update app widget from service: 要通过服务更新应用小部件,请执行以下操作:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = makeRemoteViewForWidget();
appWidgetManager.updateAppWidget(widgetId, views); //update only one widget

//Or update all widgets
for (int widgetId : ids) {
        appWidgetManager.updateAppWidget(widgetId, views);
}

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

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