更新Android主屏幕文本视图
如何在AppWidgetProvider的onReceive方法上更新主屏幕小部件的视图 我正在尝试更新主屏幕小部件的TextView,但似乎无法在onReceive方法上访问AppWidgetProvider的TextView 下面是我的onReceive的示例代码更新Android主屏幕文本视图,android,widget,homescreen,Android,Widget,Homescreen,如何在AppWidgetProvider的onReceive方法上更新主屏幕小部件的视图 我正在尝试更新主屏幕小部件的TextView,但似乎无法在onReceive方法上访问AppWidgetProvider的TextView 下面是我的onReceive的示例代码 public void onReceive(Context context,Intent intent) { final String action = intent.getAction(); if (AppWid
public void onReceive(Context context,Intent intent) {
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
} else {
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
String msg = "null";
try {
msg = intent.getStringExtra("msg");
} catch (NullPointerException e) {
}
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
// code for gathering the text to update the TextView
}
}
super.onReceive(context, intent);
}
无法更新TextView。您只能构建完整的新窗口小部件布局(RemoteView)并将其发布到主屏幕。是否有一种方法,如果我们单击主屏幕窗口小部件上的按钮,它将更新其中的文本视图?遗憾的是,没有。仅向广播接收器发送意图并重建新的RemoteView。