How to update AppWidget?

I love android and I have problems updating appwidget. This is a news widget that displays different text every 20 seconds. I have no problem getting the text to switch and display correctly when the widget is initialized. However, after every 30 minutes of updating the widget, my widgetID int array retains the int that existed before the update. Therefore, after each update, the widget shows old data and new data. Is there to clear the array of widget IDs of old data during the update process. Any help is appreciated.

My code is:

allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget); 

A way to switch text to a widget. At first, this works fine, but after the update, the old data is displayed along with the new data ...

  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); } } 

EDIT

 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); } } 
+6
source share
1 answer

Although you can be very smart with AppWidgets in android in terms of updating and so on, the easiest way is to simply restore the contents of the widget every time you update. Since you only refresh every 30 minutes, there is no reason to worry about CPU usage.

So, I propose to perform a standard rebuild every time you start the update using a standard approach. The code that you have above, although it looks correct, does not actually make you update, it is much cleaner to start from scratch. After that, if you can make it easier, tell me how to do it!

 // 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); 

Often the easiest approach (in my opinion) is to wrap this in WidgetUpdater.class, which you either call from a service or use a time call directly. Using onUpdate is usually a bad idea, as it makes the phone run at full power and is not very controlled. I never managed to get it to work. It is much better to do something like:

  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); 
+1
source

All Articles