I have a broadcast message from my service and it updates info in one of the activities but I would like it to update my Widget as well. How would I go about it?
Let's say there is a Broadcast in a system with action "ACTION" and it contains extra "TIME". How would I make the "TIME" appear in the widget? I have a clean class made by the android studio.
you just need on action in onReceive() of your broadcast receiver make a RemoteView and update widget:
public static final String ACTION = "ACTION";
public static final String TIME = "TIME";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction() != null) {
if (intent.getAction().equals(ACTION)) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
String time = intent.getStringExtra(TIME);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setTextViewText(R.id.appwidget_text, time);
appWidgetManager.updateAppWidget(widgetId, views);
}
} else {
super.onReceive(context, intent);
}
}
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.