【发布时间】:2018-07-17 04:31:41
【问题描述】:
我在 AppWidget 的按钮点击上启动了一个活动。用户在活动中输入一些数据,当用户关闭活动时,我想更新用户在 AppWidget 上的 TextView 中输入的数据。知道怎么做吗? 我已经从 AppWidget 成功启动了活动,唯一的问题是更新 AppWidget。
【问题讨论】:
我在 AppWidget 的按钮点击上启动了一个活动。用户在活动中输入一些数据,当用户关闭活动时,我想更新用户在 AppWidget 上的 TextView 中输入的数据。知道怎么做吗? 我已经从 AppWidget 成功启动了活动,唯一的问题是更新 AppWidget。
【问题讨论】:
您可以使用RemoteViews 和AppWidgetManager:
AppWidgetManager manager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(
context.getPackageName(), R.layout.widget);
remoteViews.setTextViewText(R.id.textBoxId, "new textbox content");
manager.updateAppWidget(appWidgetId, remoteViews);
【讨论】:
TextView。您写道,您希望在用户关闭 Activity 时更新您的小部件,以便 Activity 的 onPause 方法是一个好地方。
您可以从宿主应用程序活动或片段中调用 appwidget 更新方法。
这就是我的做法:
int[] ids = AppWidgetManager.getInstance(getApplication()).
getAppWidgetIds(new ComponentName(getApplication(), MyWidget.class));
MyWidget myWidget = new MyWidget();
myWidget.onUpdate(this, AppWidgetManager.getInstance(this),ids);
【讨论】: