开发者

Android update Widget from a button click

开发者 https://www.devze.com 2023-01-14 21:40 出处:网络
I have a widget that shows some informations from database. The widget is开发者_如何学JAVA periodically updated every one hour. But i also let user to update it manually by clicking a refresh button o

I have a widget that shows some informations from database. The widget is开发者_如何学JAVA 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.


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. :)


I think you have to reload your widget view when user click on *refresh butto*n.

//for refresh button
Intent refresh = new Intent(ctxt, WatchWidget_Honeycomb.class);
PendingIntent refreshIntent = PendingIntent.getBroadcast(ctxt, 0, refresh, 0);
widget.setOnClickPendingIntent(R.id.btn_Refresh, refreshIntent); 
appWidgetManager.updateAppWidget(new ComponentName(ctxt,WatchWidget_Honeycomb.class), widget);

If You are getting any problem then put comment.

Happy Coding.

0

精彩评论

暂无评论...
验证码 换一张
取 消