0

The code here in onHandleIntent:

RemoteViews views = new RemoteViews(getPackageName(), R.layout.mylayout);
views.setTextViewText(R.id.txtView1, "some string");
...
Bundle bundle = intent.getExtras();
int id = bundle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(id, views);

does not update the widget's TextView named txtView1. I've searched and searched, but it appears that everything I've done seems to match what I found.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
joe
  • 1,125
  • 9
  • 18
  • 3
    Service runs in the background You cannot update ui from service directly. Use a handler – Raghunandan Apr 12 '13 at 06:42
  • Yes, agreed. This is also why Toasts don't work. However, RemoteViews could be in another process. The answer by @Harshid almost works, but it updates all instances. – joe Apr 14 '13 at 01:50

3 Answers3

0

You need Handler to update UI from worker thread.

You can take help from this tutorial http://crodrigues.com/updating-the-ui-from-a-background-thread-on-android/

or alternatively you can use Asynctask if you don't want to get into thread and handler stuff.

Asynctask manages this stuff by itself and you can update UI in onPostExecute() method and so on..

You can read details about asynctask in developer android site of google.

Sunny
  • 1,441
  • 2
  • 13
  • 22
  • [this stackoverflow link](http://stackoverflow.com/questions/13491049/start-async-task-from-onhandleintent) might also help – Sunny Apr 12 '13 at 06:55
0

You have to update your textview in app widget development.

  RemoteViews views = new RemoteViews(getPackageName(), R.layout.mylayout);
    views.setTextViewText(R.id.txtView1, "some string");
    thisWidget = new ComponentName(context, WatchWidget.class);  
    appWidgetManager.updateAppWidget(thisWidget, remoteViews);
Harshid
  • 5,701
  • 4
  • 37
  • 50
  • Thank you @Harshid, this gets me farther than before, however, it updates all instances at the same time. – joe Apr 14 '13 at 01:51
0

Sigh, thank you all for your answers, unfortunately it was my mistake. In my onUpdate from my custom AppWidgetProvider, I was passing the wrong Id. I've switched it now to the proper Id, and each widget instance updates properly.

joe
  • 1,125
  • 9
  • 18