Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android update AppWidget after Volley service

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();
            }
        }
);
like image 953
Danny Buonocore Avatar asked Jul 03 '18 08:07

Danny Buonocore


2 Answers

You can update android views with UI thread only.

Try updating widget in UI thread like.

new Handler(Looper.getMainLooper()).post(...runnable)
like image 57
Khemraj Sharma Avatar answered Sep 23 '22 15:09

Khemraj Sharma


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

 manager.notifyAppWidgetViewDataChanged(ids, views);
like image 33
Sabbir Ahmed Avatar answered Sep 23 '22 15:09

Sabbir Ahmed