好的,所以我有一个Android小部件,我一直在android小部件更新的麻烦。我使用了一些简单的硬编码setTextViewText
但是当我调试应用程序时没有任何事情发生..
我的代码:
public class Widget extends AppWidgetProvider {
RemoteViews views;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int N = appWidgetIds.length;
views = new RemoteViews(context.getPackageName(), R.layout.widget);
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
String alarm = Settings.System.getString(context.getContentResolver(),
Settings.System.NEXT_ALARM_FORMATTED);
views.setTextViewText(R.id.tvAlarm, alarm);
views.setTextViewText(R.id.tvNextAlarm, "qwertyuiop");
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
我知道有些代码是错误的,例如闹钟,我试图以某种方式加入电池状态,但我被告知BroadcastReceiver
不是前进的方式所以此刻我已经评论过了。
但主要问题是更新小部件以更改TextView
无效......
答案 0 :(得分:2)
我刚在我的一个小部件中尝试过这个代码,它运行正常:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// Get ids of all the instances of the widget
ComponentName widget = new ComponentName(context, MediaWidgerProvider.class);
int[] widgetIds = appWidgetManager.getAppWidgetIds(widget);
for (int widgetId : widgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_widget);
remoteViews.setTextViewText(R.id.widget_text, "Hello");
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}