对于许多时间回答的问题感到抱歉,但对我来说现在仍在工作: - (
我有以下活动代码:
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int count = appWidgetIds.length;
for (int i = 0; i < count; i++) {
int widgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.simple_widget);
Intent intent = new Intent(context, SimpleWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
new Weather(remoteViews).execute();
remoteViews.setOnClickPendingIntent(R.id.actionButton, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
使用Async Weather类但这不起作用。 remoteViews中缺少值。
class Weather extends AsyncTask<Void, String, String> {
private RemoteViews remoteViews ;
public Weather(RemoteViews remoteViews) {
this.remoteViews = remoteViews;
}
@Override
protected String doInBackground(Void... arg0) {
return "COLD";
}
@Override
protected void onPostExecute(String temp) {
remoteViews.setTextViewText(R.id.textView, temp);
}
答案 0 :(得分:1)
您传递给RemoteViews
的{{1}}当时将使用 。在您的情况下,updateAppWidget()
任务在Weather
被使用之前甚至不会开始,因此您对RemoteViews
的更改没有意义。
相反:
切换到RemoteViews
,因为此处使用Thread
没有价值,
在AsyncTask
updateAppWidget()