2013-02-21 50 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