Android update AppWidget after Volley service
Asked Answered
D

2

8

The Configuration Activity for my widget starts a service to make an http request and update the widget views. I can update the views from the service just fine (see below), but why is it I cannot update them from the response handler?

public class TestService extends Service {

    @SuppressWarnings("unused")
    private static final String TAG = "TestService";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Service started");

        final Context context = this;
        final RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_activity);
        final ComponentName thisWidget = new ComponentName(context, WidgetActivity.class);

        final RequestQueue queue = Volley.newRequestQueue(this);
        final String url = "http://google.com";

        final AppWidgetManager manager = AppWidgetManager.getInstance(context);
        final int[] ids = manager.getAppWidgetIds(thisWidget);

        StringRequest request = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        // DOES NOT WORK
                        views.setTextViewText(R.id.last_updated_text, "After Request");
                        manager.partiallyUpdateAppWidget(ids, views);

                        // ALSO DOES NOT WORK
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                views.setTextViewText(R.id.last_updated_text, "After Request");
                                manager.partiallyUpdateAppWidget(ids, views);
                            }
                        });

                        // ALSO DOES NOT WORK
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                views.setTextViewText(R.id.last_updated_text, "After Request");
                                AppWidgetManager.getInstance(context).updateAppWidget(thisWidget, views);
                            }
                        });

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }
        );

        queue.add(request);

        // WORKS
        views.setTextViewText(R.id.last_updated_text, "Before Request");
        manager.partiallyUpdateAppWidget(ids, views);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

EDIT

I restarted my device and now the following seems to work just fine:

StringRequest priceRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Result: " + response);

                views.setTextViewText(R.id.last_updated_text, "After Request");
                AppWidgetManager.getInstance(context).updateAppWidget(thisWidget, views);

            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }
);
Dyne answered 3/7, 2018 at 8:43 Comment(0)
B
1

You can update android views with UI thread only.

Try updating widget in UI thread like.

new Handler(Looper.getMainLooper()).post(...runnable)
Borehole answered 3/7, 2018 at 8:47 Comment(5)
Updated my question to include your code, still does not seem to work :/Dyne
I added that to my question as well, no dice. That is how I was originally attempting to update the widget.Dyne
Did you debug this service, is it alive when response arrive?Borehole
Restarted my device and now it seems to work fine even without using an additional handler or anything... weird.Dyne
Really weird :)Borehole
A
1

When we updater AppWidget data we need to call this method -> notifyAppWidgetViewDataChanged(int[] appWidgetIds, int viewId).

 manager.notifyAppWidgetViewDataChanged(ids, views);
Abridgment answered 3/7, 2018 at 8:49 Comment(1)
Thanks, but I got it. Just restarted my device and the code seemed fine. I will try this if I have any future issues with itDyne

© 2022 - 2024 — McMap. All rights reserved.