当我尝试更新内容时,我的 App-Widget 出现问题。
小部件看起来像这样:http: //i.stack.imgur.com/tEGLv.jpg
Widget 由一个 Header (Buttons + TextViews) 和一个 ListView 组成。如果用户按下按钮,ListView 会显示第二天的内容,一切正常。但问题是,如果我尝试从我的应用程序更新小部件或 ListView 不会更新的自动时间段。
这是我的代码。
应用小部件提供者:
public class StudyWidgetProvider extends AppWidgetProvider{
private static String BUTTON_BACK = "BUTTON_BACK";
private static String BUTTON_FORWARD = "BUTTON_FORWARD";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context.startService(new Intent(context, UpdateService.class));
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(WidgetViewsFactory.currentDate == null){
WidgetViewsFactory.currentDate = Calendar.getInstance();
}
String action = intent.getAction();
if(action != null){
if(action.equals(BUTTON_BACK)){
WidgetViewsFactory.currentDate.add(Calendar.DATE, -1);
}
if(action.equals(BUTTON_FORWARD)){
WidgetViewsFactory.currentDate.add(Calendar.DATE, 1);
}
}
context.startService(new Intent(context, UpdateService.class));
}
}
处理 Widget 更新的 UpdateService。
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(LOG_TAG, "Update-Service started");
RemoteViews widget = buildUpdate(this);
ComponentName thisWidget = new ComponentName(this, StudyWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.notifyAppWidgetViewDataChanged(manager.getAppWidgetIds(thisWidget), R.id.words);
manager.updateAppWidget(thisWidget, widget);
Log.i(LOG_TAG, "The Content!");
if(widgetObjects == null){
Log.i(LOG_TAG, "WidgetObjects sind NULL");
} else {
Log.i(LOG_TAG, "WidgetObjects sind NICHT-NULL");
for(int i=0; i<widgetObjects.size(); i++){
Log.i(LOG_TAG, "--- " + widgetObjects.get(i).getName());
}
}
Log.i(LOG_TAG, "Update-Service ended");
}
public RemoteViews buildUpdate(Context context){
RemoteViews retVal = new RemoteViews(context.getPackageName(), R.layout.widget);
if(WidgetViewsFactory.currentDate == null){
WidgetViewsFactory.currentDate = Calendar.getInstance();
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), StudyWidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
loadSDCardContent(context, appWidgetManager, appWidgetIds);
//Setzen des Widget-UIs
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
retVal.setRemoteAdapter(R.id.words, intent);
SimpleDateFormat dateFormater = new SimpleDateFormat("EEEE");
retVal.setTextViewText(R.id.widget_day, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));
dateFormater = new SimpleDateFormat("dd.MM.yyyy");
retVal.setTextViewText(R.id.widget_date, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));
//Button-Actions
Intent backIntent = new Intent(context, StudyWidgetProvider.class);
backIntent.setAction(BUTTON_BACK);
Intent forwardIntent = new Intent(context, StudyWidgetProvider.class);
forwardIntent.setAction(BUTTON_FORWARD);
PendingIntent backPendingIntent = PendingIntent.getBroadcast(context, 0, backIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent forwardPendingIntent = PendingIntent.getBroadcast(context, 0, forwardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
retVal.setOnClickPendingIntent(R.id.widget_but_back, backPendingIntent);
retVal.setOnClickPendingIntent(R.id.widget_but_forward, forwardPendingIntent);
Intent clickIntent = new Intent(context, WidgetActivity.class);
PendingIntent clickPI=PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
retVal.setPendingIntentTemplate(R.id.words, clickPI);
return retVal;
}
下面是我如何从我的应用程序更新小部件:
public RemoteViews buildUpdate(Context context){
RemoteViews retVal = new RemoteViews(context.getPackageName(), R.layout.widget);
if(WidgetViewsFactory.currentDate == null){
WidgetViewsFactory.currentDate = Calendar.getInstance();
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), StudyWidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
loadSDCardContent(context, appWidgetManager, appWidgetIds);
//Setzen des Widget-UIs
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
retVal.setRemoteAdapter(R.id.words, intent);
SimpleDateFormat dateFormater = new SimpleDateFormat("EEEE");
retVal.setTextViewText(R.id.widget_day, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));
dateFormater = new SimpleDateFormat("dd.MM.yyyy");
retVal.setTextViewText(R.id.widget_date, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));
//Button-Actions
Intent backIntent = new Intent(context, StudyWidgetProvider.class);
backIntent.setAction(BUTTON_BACK);
Intent forwardIntent = new Intent(context, StudyWidgetProvider.class);
forwardIntent.setAction(BUTTON_FORWARD);
PendingIntent backPendingIntent = PendingIntent.getBroadcast(context, 0, backIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent forwardPendingIntent = PendingIntent.getBroadcast(context, 0, forwardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
retVal.setOnClickPendingIntent(R.id.widget_but_back, backPendingIntent);
retVal.setOnClickPendingIntent(R.id.widget_but_forward, forwardPendingIntent);
Intent clickIntent = new Intent(context, WidgetActivity.class);
PendingIntent clickPI=PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
retVal.setPendingIntentTemplate(R.id.words, clickPI);
return retVal;
}
奇怪的是,当我按下小部件的按钮时,一切都按我的意愿工作。我的列表视图的 onDatasetChanged 方法被调用并且内容正在改变,但是如果我从我的应用程序中尝试这个,没有任何反应
我希望你们中的某个人知道为什么这不起作用。我现在坚持了2天,我不知道......
感谢您的帮助!
编辑 我发现这不适用于我的设备(带有 miui v4 的 htc),而是在模拟器上。
编辑 2 我在我的设备上安装了 Android 4(带有 Sense 4),之后它也可以工作了。看起来 MIUI 是问题所在。