简体   繁体   中英

How to I feed Widget with Background Service Android

I have the widget property in my application. I show the data from the location on the widget but do not show the current data on the layout after 3 4 hours. I can add it to the homepage without problems, but it stops after 3 or 4 hours. 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". 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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