Android 在小部件中更新textView
我读了很多问答,但找不到答案。Android 在小部件中更新textView,android,android-intent,widget,textview,remoteview,Android,Android Intent,Widget,Textview,Remoteview,我读了很多问答,但找不到答案。 可能是我的实现有问题。 问题是小部件中的myTextView没有得到更新 逻辑是这样的: 1.setonclickpendingent在onUpdate()中的特定按钮上 2.点击此按钮将广播一个意图和一个声明的动作 3.最后,我将更新onreceive()中textView的文本 Widget2.class: public class Widget2 extends AppWidgetProvider { private static final String
可能是我的实现有问题。
问题是小部件中的my
TextView
没有得到更新
逻辑是这样的:
1.setonclickpendingent
在onUpdate()中的特定按钮上
2.点击此按钮将广播一个意图
和一个声明的动作
3.最后,我将更新onreceive()中textView
的文本
Widget2.class:
public class Widget2 extends AppWidgetProvider {
private static final String SCROLL_LEFT = "widget2.SCROLL_LEFT";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.small);
Intent scrollLeft = new Intent(Widget2.SCROLL_LEFT);
PendingIntent leftScrollPendingIntent = PendingIntent.getBroadcast(context,
appWidgetId, scrollLeft, appWidgetId);
remoteViews.setOnClickPendingIntent(R.id.left_scroller, leftScrollPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
@Override
public void onReceive(Context context, Intent intent) {
Log.e(getClass().getSimpleName(), "onReceive()");
int appWidgetId = intent.getFlags();
if (intent.getAction().equals(SCROLL_LEFT)) {
updateCurrentWidget(context, appWidgetId);
Log.i("onReceive", SCROLL_LEFT + " appWidgetId = " + appWidgetId);
}
super.onReceive(context,intent);
}
private void updateCurrentWidget(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.small);
remoteViews.setString(R.id.name_of_the_app, "setText", "android os");
remoteViews.setTextViewText(R.id.description, "best ever");
Log.i("updateCurrentWidget", "the text have been set");
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(appWidgetId, remoteViews);
}
一切似乎都是正确的,但文字从未改变 您正在PendingEvent标志中设置appWidgetId。这是错误的,因为PendingTet标志有意义,它们并不意味着要保存一些数据
您应该使用额外功能。小部件中的关键是,在调用updateAppWidget()方法之前,您必须设置每个视图
和每个意图
。在这里,您可以从onReceive()
方法调用updateWidgetInstance(…)
:
// action = the String that you get from your intent in the onRecive() method
// id = the id of the appWidget instance that you want to update
// you can get the id in the onReceive() method like this:
// int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
private static void updateWidgetInstance(Context context, AppWidgetManager manager, String action, int id) {
RemoteViews remoteViews = updateCurrentWidget(context, id);
remoteViews = setIntents(remoteViews, context, id);
manager.updateAppWidget(id, remoteViews);
}
更新视图:(在此,您还可以根据需要更改此实例的布局)
更新意向:
private static RemoteViews setIntents(RemoteViews rm, Context context, int appWidgetId) {
Intent click = new Intent(context, Widget.class);
click.setAction("[name of the action]");
click.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, appWidgetId, click, PendingIntent.FLAG_UPDATE_CURRENT);
rm.setOnClickPendingIntent(R.id.button1, pendingIntent);
return rm;
}
您的onUpdate方法从未被调用,因为您没有调用super.onReceive
。它被调用了,我忘了在这里复制它。现在它是正确的
// action = the String that you get from your intent in the onRecive() method
// id = the id of the appWidget instance that you want to update
// you can get the id in the onReceive() method like this:
// int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
private static void updateWidgetInstance(Context context, AppWidgetManager manager, String action, int id) {
RemoteViews remoteViews = updateCurrentWidget(context, id);
remoteViews = setIntents(remoteViews, context, id);
manager.updateAppWidget(id, remoteViews);
}
private static RemoteViews updateCurrentWidget(Context context, int appWidgetId) {
RemoteViews remoteViews;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_small_layout);
remoteViews.setTextViewText(R.id.widget_name_of_the_app, "Android OS");
remoteViews.setTextViewText(R.id.widget_app_description, "best ever");
remoteViews.setImageViewUri(R.id.widget_icon_of_the_app, ...);
return remoteViews;
}
private static RemoteViews setIntents(RemoteViews rm, Context context, int appWidgetId) {
Intent click = new Intent(context, Widget.class);
click.setAction("[name of the action]");
click.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, appWidgetId, click, PendingIntent.FLAG_UPDATE_CURRENT);
rm.setOnClickPendingIntent(R.id.button1, pendingIntent);
return rm;
}