Skip to content Skip to sidebar Skip to footer

Appwidget Refresh Uri For Remoteviews

I have created an Appwidget that displays an image file (test.png) that is provided to it's RemoteViews via Uri. In onUpdate i run a service that changes the content of the file.

Solution 1:

There are various things that I have found can make widgets hard.

A. onUpdate isn't really an update mechanism

Contrary to how it sounds, onUpdate is only called in two situations:

  1. When the widget is created.
  2. Whenever the update cycle time (updateMillis defined in the xml definition file file for the widget) elapses.

Key point: onUpdate is never called at other times. (As far as I have ever seen in practice).

If you want the widget to update at another time, it is necessary to create a separate mechanism with knowledge of the widget and the capacity to be triggered. Typically this would be a Service which you start in the onUpdate routine. This might look like:

publicvoidonUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
{       
    // start the service in case it ain't started yet, this also force a widget update to ensure correct status on them
    Intent intent = new Intent(context, MyService.class);
    intent.putExtra('service.startCode', /*A number to identify what is going on*/);
    context.startService(intent);

    // Everything else is triggered from the service!!
}

The service then sets the content sof the widget, and updates them as necessary, either through internal timekeeping or through the use of the Broadcast mechanism.

B. You can't really update a bit of the widget

It might seem logical to create a remoteViews when you create the widget and then update that, or the Views in it, when things change. In my experience this doesn't work predictably. When you want to change anything in a widget, create a new remoteViews, fill it out correctly and then assign it to the widget.

Solution 2:

I ran into some device dependency with the way RemoteView was handling URIs, and found my way to a solution like this:

RemoteViewsremoteViews=newRemoteViews(context.getPackageName(),R.layout.widget_layout);
Uriuri= Uri.parse("file://"+Environment.getExternalStorageDirectory().getPath()+"/test/test.png");

Bitmapbitmap= BitmapFactory.decodeResource(context.getResources(), R.drawable.crap);
bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
remoteViews.setImageViewBitmap(R.id.IV_widget_image, bitmap);

It also eliminated the need to cycle RemoteViews, as in my other answer.

Solution 3:

Certainly not elegant, or memory efficient, but I found that if I duplicated my RemoteViews

RemoteViewsremoteViews=newRemoteViews(context.getPackageName(),R.layout.widget_layout01);
RemoteViewsremoteViews2=newRemoteViews(context.getPackageName(),R.layout.widget_layout02);

duplicate the code surrounding them, and swap them intermittently between image updates (kind of like a buffer), resetting the RemoteViews each time forces an update on their content!

Works for me for now, please feel free chime in with a more correct solution.

Post a Comment for "Appwidget Refresh Uri For Remoteviews"