B. *_*ney 6 int android updates android-widget android-appwidget
我是Android的菜鸟,我在更新appwidget时遇到问题.它是一个新闻小部件,每20秒显示不同的文本.初始化窗口小部件时,我可以正确切换和显示文本.但是,在每30分钟更新一次窗口小部件之后,我的widgetID int数组会保留更新之前存在的int.因此,每次更新后,窗口小部件都会显示旧数据和新数据.是否在更新过程中清除小部件ID int旧数据数组.任何帮助是极大的赞赏.
我的代码:
allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
Run Code Online (Sandbox Code Playgroud)
在窗口小部件中切换文本的方法 这最初工作正常,但更新后显示旧数据和新数据...
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); }
}
Run Code Online (Sandbox Code Playgroud)
编辑
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); }
}
Run Code Online (Sandbox Code Playgroud)
尽管您可以在更新等方面非常巧妙地使用 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);
Run Code Online (Sandbox Code Playgroud)
通常,最简单的方法(在我看来)是将其包装在 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4379 次 |
| 最近记录: |