2013-02-21 51 views
6

我是android的noob,我遇到更新appwidget的问题。这是一个每20秒显示不同文本的新闻小部件。在小部件初始化时,我可以正确地切换&显示文字。但是,在每隔30分钟更新一次widget后,我的widgetID int数组将保留更新前存在的int。所以在每次更新之后,小部件会显示旧数据和新数据。是否有更新过程中清除旧数据的小部件ID int数组。任何帮助是极大的赞赏。如何正确更新AppWidget?

我的代码:

在小部件切换文本
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

也许如果你设置其他{storyCounter ++; } to storyConter = 0;会做的伎俩。 – k0sh 2013-02-21 16:15:08

+0

感谢您的回复。这将如何防止以前的widgetid的数据被显示? – 2013-02-21 16:17:15

+0

我从来没有使用过小部件,但我使用位图很多。当我想重置某个位图时,我会调用bmp = null;我不确定你想在那里做什么。但你调用storyCounter ++;当storyCounter = 0时;并在你的其他声明。这意味着你清除了你的storyCounter = 0;然后你调用storyCounter ++;为什么要这么做 ?从逻辑上讲,如果你在其他语句中你必须做不同的事情,但在你的事情中你做了两次同样的事情。 – k0sh 2013-02-21 16:22:21

回答

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); 
+0

谢谢你的建议。我已经在我的小部件提供程序类的onUpdate方法中更新了我的小部件。请检查我的编辑,看看我是否正确处理它。 – 2013-02-21 16:51:07

+0

我已经扩展了答案,试图澄清为什么我提出了另一种方法 - 如果还不清楚,请说。 – 2013-02-21 17:32:56

+0

你现在整理了吗? – 2013-02-22 16:31:10