tags:

views:

51

answers:

0

Hello to all,

I decided to create my first Android widget this weekend and although I have gotten it to work somewhat, it does not display the dynamically-loaded content until I rotate the device (using Launcher Pro Plus which allows landscape orientation and rotation of home screens).

The widget is pretty simple, it just retrieves an image from a website and displays the image. I created a service that is started in onUpdate() of the AppWidgetProvider subclass. The service then retrieves the image, builds the RemoteViews object and then calls updateAppWidget() on the AppWidgetManager. Here is the code for the widget:

public class PropagationWidget extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    context.startService(new Intent(context, UpdateService.class));
}
public static class UpdateService extends IntentService {

    public UpdateService() {
        super("PropagationWidget$UpdateService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ComponentName me=new ComponentName(this,
                 PropagationWidget.class);
        AppWidgetManager mgr=AppWidgetManager.getInstance(this);

        mgr.updateAppWidget(me, buildUpdate(this));
    }

    public RemoteViews buildUpdate(Context context) {
        RemoteViews views = null;

        URL imageUrl = null;

        try {
            views = new RemoteViews(context.getPackageName(),
                    R.layout.widget_layout);

            imageUrl = new URL(
                    "http://www.mountainlake.k12.mn.us/ham/aprs/path.cgi?map=na&img=node&freq=144&type=image");

            InputStream is = (InputStream) imageUrl.getContent();
            Bitmap image = BitmapFactory.decodeStream(is);
            views.setImageViewBitmap(R.id.image, image);

            Time today = new Time();
            today.setToNow();

            views.setTextViewText(R.id.last_updated, "Last updated: " + today.format("%D %r"));
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return views;
    }
}

When the widget loads, I only get the frame of the widget, but no content, which makes sense because UpdateService still hasn't retrieved the image from the website yet. However, even after UpdateService has finished retrieving the image and immediate calls updateAppWidget(), the image still does not appear. When I rotate the device, however, the image appears, and will remain upon subsequent rotations.

Also too, the widget is set in the appwidget-provider XML to update every 30 minutes. These updates do not show up until I rotate the device, and when the widget is redrawn, I then see the update.

Is there something really basic here that I'm missing? Any insight would be greatly appreciated! Please let me know if I should provide any additional information to help with this.

Thanks for your time!

Justin