主屏幕小部件文本视图未更新
Posted
技术标签:
【中文标题】主屏幕小部件文本视图未更新【英文标题】:home screen widget text view not updating 【发布时间】:2013-05-09 15:09:16 【问题描述】:这是我的程序的代码 -
package com.nadeem.todos;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.RemoteViews;
public class todo_widget extends AppWidgetProvider
private SQLiteDatabase db;
private TodoSQLiteHelper dbHelper;
String[] widget_title = "", "", "", "", "", "" ;
String[] widget_content = "", "", "", "", "", "" ;
public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh";
public static String ACTION_WIDGET_OPEN = "ActionReceiverOpen";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.contentw2, pendingIntent);
views.setOnClickPendingIntent(R.id.contentw3, pendingIntent);
Intent i1 = new Intent(context, todo_widget.class);
i1.setAction(ACTION_WIDGET_REFRESH);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i1,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.flip, pi);
views.setOnClickPendingIntent(R.id.contentw1, pi);
appWidgetManager.updateAppWidget(appWidgetIds, views);
@Override
public void onReceive(Context context, Intent intent)
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
int[] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(
new ComponentName(context, todo_widget.class));
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
if (intent.getAction().equals(ACTION_WIDGET_REFRESH))
views.setTextViewText(R.id.titlew1, widget_title[3]);
views.setTextViewText(R.id.contentw1, widget_content[3]);
appWidgetManager.updateAppWidget(ids, views);
else if (intent.getAction().equals(
AppWidgetManager.ACTION_APPWIDGET_UPDATE))
dbHelper = new TodoSQLiteHelper(context);
db = dbHelper.getWritableDatabase();
String[] tableColumns = new String[] "_id", "title", "todo" ;
int j = 0;
Cursor cursor = db.query("todos", tableColumns, null, null, null,
null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast())
if (j < 6)
widget_title[j] = cursor.getString(1);
widget_content[j] = cursor.getString(2);
j++;
cursor.moveToNext();
db.close();
final int N = ids.length;
for (int i = 0; i < N; i++)
int appWidgetId = ids[i];
views.setTextViewText(R.id.titlew1, widget_title[0]);
views.setTextViewText(R.id.titlew2, widget_title[1]);
views.setTextViewText(R.id.titlew3, widget_title[2]);
views.setTextViewText(R.id.contentw1, widget_content[0]);
views.setTextViewText(R.id.contentw2, widget_content[1]);
views.setTextViewText(R.id.contentw3, widget_content[2]);
appWidgetManager.updateAppWidget(appWidgetId, views);
super.onReceive(context, intent);
我的问题是当 Action_widget_refresh 为真时,onRecieve 方法中的文本视图没有得到更新。我在文本视图上设置了待处理的意图,以便在单击它时文本视图(R.id.content1)的文本会发生变化。但是当我点击文本视图时,文本视图消失了。我不知道为什么会这样。除此之外,代码运行良好。
【问题讨论】:
【参考方案1】:每次调用appWidgetManager.updateAppWidget()
时,您传入的 RemoveViews 都需要“包含小部件的完整表示”。这意味着您每次都需要为 TextViews 设置文本,而不仅仅是在文本应该更改时。如果只设置更改的文本,您将失去想要保持不变的文本。
【讨论】:
【参考方案2】:我认为你应该这样做:
if (intent.getAction().equals(ACTION_WIDGET_REFRESH))
views.setTextViewText(R.id.titlew1, widget_title[3]);
views.setTextViewText(R.id.contentw1, widget_content[3]);
final int N = ids.length;
for (int i = 0; i < N; i++)
int appWidgetId = ids[i];
appWidgetManager.updateAppWidget(appWidgetId, views);
【讨论】:
以上是关于主屏幕小部件文本视图未更新的主要内容,如果未能解决你的问题,请参考以下文章
如何在不完全影响主屏幕绘图性能的情况下每 200/250 毫秒更新一次小部件?