我是Android的菜鸟,我在更新appwidget时遇到问题。它是一个新闻小部件,每20秒显示不同的文本。我没有问题让文本切换&初始化窗口小部件时正确显示。但是,在每30分钟更新一次窗口小部件之后,我的widgetID int数组会保留更新之前存在的int。因此,每次更新后,窗口小部件都会显示旧数据和新数据。是否在更新过程中清除小部件ID int旧数据数组。非常感谢任何帮助。
我的代码:
allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
在窗口小部件中切换文本的方法。这最初工作正常,但更新后显示旧数据和新数据......
public void updateStory() {
tickerheadline = RssReader.rssheadline.get(storyCounter);
tickerstory = RssReader.rssstory.get(storyCounter);
remoteViews.setTextViewText(R.id.headline, tickerheadline );
remoteViews.setTextViewText(R.id.story, tickerstory );
if (storyCounter==RssReader.rssheadline.size()-1){
storyCounter = 0;
storyCounter++;
}else{
storyCounter++;
}
appWidgetManager.updateAppWidget(allWidgetIds, remoteViews);
//Log.e("Widget", allWidgetIds.toString());
mHandler.postDelayed(new Runnable() {
public void run() {
updateStory();
} } ,20000); }
}
修改
public void updateStory() {
//Added
appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
ComponentName thisWidget = new ComponentName(getApplicationContext(),MyWidgetProvider.class);
remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(),R.layout.widget1);
tickerheadline = RssReader.rssheadline.get(storyCounter);
tickerstory = RssReader.rssstory.get(storyCounter);
remoteViews.setTextViewText(R.id.headline, tickerheadline );
remoteViews.setTextViewText(R.id.story, tickerstory );
if (storyCounter==RssReader.rssheadline.size()-1){
storyCounter = 0;
storyCounter++;
}else{
storyCounter++;
}
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
//appWidgetManager.updateAppWidget(allWidgetIds, remoteViews);
//Log.e("Widget", allWidgetIds.toString());
mHandler.postDelayed(new Runnable() {
public void run() {
updateStory();
} } ,20000); }
}
答案 0 :(得分:1)
虽然在更新等方面你可以非常聪明地使用Android中的AppWidgets,但最简单的方法就是每次刷新时重建小部件内容。因为你每30分钟只刷新一次,所以没有太多理由担心cpu的使用。
因此,我建议每次使用标准方法触发更新时进行标准重建。你上面的代码,虽然它看起来是正确的,但实际上并没有强制更新,从头开始更清洁。一旦它工作,那么如果你可以使它更轻量级,请告诉我如何!
// You need to provide:
// myContext - the Context it is being run in.
// R.layout.widget - the xml layout of the widget
// All the content to put in it (duh!)
// Widget.class - the class for the widget
RemoteViews rv= new RemoteViews(myContext.getPackageName(), R.layout.widget);
rv.setXXXXXXX // as per normal using most recent data set.
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(myContext);
// If you know the App Widget Id use this:
appWidgetManager.updateAppWidget(appWidgetId, rv);
// Or to update all your widgets with the same contents:
ComponentName projectWidget = new ComponentName(myContext, Widget.class);
appWidgetManager.updateAppWidget(projectWidget, rv);
通常最简单的方法(在我看来)是将它包装在WidgetUpdater.class中,您可以从服务中调用它,也可以直接使用时间警报调用。使用onUpdate通常是一个坏主意,因为它会强制手机进入全功率模式并且不是很可控。我从未设法让它发挥作用。做更好的事情,比如:
Intent myIntent = // As per your onUpdate Code
alarmPendingIntent = PendingIntent.getService(context, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
// ... using the alarm manager mechanism.
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdateTimeMillis, alarmPendingIntent);