Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android update Widget from a button click

I have a widget that shows some informations from database. The widget is periodically updated every one hour. But i also let user to update it manually by clicking a refresh button on widget.How can i perform the click action and refresh the widget?

Note: The widget uses service to perform operations.

Thanx in advance.

like image 408
Lorensius W. L. T Avatar asked Sep 03 '10 06:09

Lorensius W. L. T


1 Answers

I've looked for an answer for hours and finally I've figured it out. In your AppWidgetProvider, in the onUpdate, loop in your widgets and, for each widget, create an Intent (add to it the widget id and some data to make it unique, like the intent uri), then create a PendingIntent and assign it to the view where you want to create the click listener.

    for(int i = 0; i<appWidgetIds.length; i++) {
        Intent serviceIntent = new Intent(context, MyService.class);
        serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingServiceIntent = PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.refresh, pendingServiceIntent);

        context.startService(serviceIntent);
        appWidgetManager.updateAppWidget(appWidgetIds[i], views);
    }

In my specific case I had two click listener, one to refresh and one to start the settings activity, so if you want to start an Activity just use the PendingIntent.getActivity and add the Intent.FLAG_ACTIVITY_NEW_TASK flag.

The two combined:

    for(int i = 0; i<appWidgetIds.length; i++) {
        Intent serviceIntent = new Intent(context, MyService.class);
        serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingServiceIntent = PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent settingsIntent = new Intent(context, SettingsActivity.class);
        settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        settingsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        settingsIntent.setData(Uri.parse(settingsIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingSettingsIntent = PendingIntent.getActivity(context, 0, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.title, pendingSettingsIntent);
        views.setOnClickPendingIntent(R.id.refresh, pendingServiceIntent);

        context.startService(serviceIntent);
        appWidgetManager.updateAppWidget(appWidgetIds[i], views);
    }

Hope this will help someone. :)

like image 196
Enrichman Avatar answered Sep 17 '22 20:09

Enrichman