[英]How to update widget based on Broadcast message?
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". 假设系统中有一个动作为“ ACTION”的广播,其中包含额外的“ TIME”。 How would I make the "TIME" appear in the widget?
如何使“ TIME”出现在小部件中? I have a clean class made by the android studio.
我有一个由android studio制作的干净类。
you just need on action in onReceive() of your broadcast receiver make a RemoteView and update widget: 您只需要在广播接收器的onReceive()中进行操作即可制作一个RemoteView并更新小部件:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.