Button click event for android widget

后端 未结 5 1193
醉酒成梦
醉酒成梦 2021-01-30 13:16

I have an android widget that fetches data from a server every 10 minutes and display\'s it on the screen.
I\'d like to add a \"Refresh\" button to that widget.
When th

5条回答
  •  渐次进展
    2021-01-30 13:49

    Here is another answer with the following benefits:

    • It handles all App Widget instances (a user might have multiple instances of your widget in various configurations/sizes on your screen). Coding for all instances is what the official documentation prescribes. See Guide > App Widgets > Using the AppWidgetProvider Class , scroll down to the code example for "ExampleAppWidgetProvider".
    • The workhorse code in onReceive in effect calls onUpdate (so you reduce code duplication).
    • The code in onUpdate(Context context) is generalised so that it can be dropped into any AppWidgetProvider subclass.

    The code:

    public class MyWidget extends AppWidgetProvider {
    
        private static final String ACTION_UPDATE_CLICK = 
                  "com.example.myapp.action.UPDATE_CLICK";
    
        private static int mCount = 0;
    
        private static String getMessage() {
            return String.valueOf(mCount++);
        }
    
        private PendingIntent getPendingSelfIntent(Context context, String action) {
            // An explicit intent directed at the current class (the "self").
            Intent intent = new Intent(context, getClass());
            intent.setAction(action);
            return PendingIntent.getBroadcast(context, 0, intent, 0);
        }
    
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                             int[] appWidgetIds) {
            super.onUpdate(context, appWidgetManager, appWidgetIds);
    
            String message = getMessage();
    
            // Loop for every App Widget instance that belongs to this provider.
            // Noting, that is, a user might have multiple instances of the same
            // widget on
            // their home screen.
            for (int appWidgetID : appWidgetIds) {
                RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                                                          R.layout.my_widget);
    
                remoteViews.setTextViewText(R.id.textView_output, message);
                remoteViews.setOnClickPendingIntent(R.id.button_update,
                                                    getPendingSelfIntent(context,
                                                               ACTION_UPDATE_CLICK)
                );
    
                appWidgetManager.updateAppWidget(appWidgetID, remoteViews);
    
            }
        }
    
        /**
         * A general technique for calling the onUpdate method,
         * requiring only the context parameter.
         *
         * @author John Bentley, based on Android-er code.
         * @see 
         * Android-er > 2010-10-19 > Update Widget in onReceive() method
         */
        private void onUpdate(Context context) {
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance
                    (context);
    
            // Uses getClass().getName() rather than MyWidget.class.getName() for
            // portability into any App Widget Provider Class
            ComponentName thisAppWidgetComponentName =
                    new ComponentName(context.getPackageName(),getClass().getName()
            );
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
                    thisAppWidgetComponentName);
            onUpdate(context, appWidgetManager, appWidgetIds);
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            super.onReceive(context, intent);
    
            if (ACTION_UPDATE_CLICK.equals(intent.getAction())) {
                onUpdate(context);
            }
        }
    
    }
    

    The widget looks like this

    Widget update button example. Simple counting.

    This builds on the getPendingSelfIntent work of @Kels, @SharonHaimPour and @Erti-ChrisEelmaa.

    It also builds on Android-er > 2010-10-19 > Update Widget in onReceive() method (not me) where it is demonstrated how to call onUpdate from onReceive, on an App Widget instance basis. I make that code general and wrap it in callOnUpdate.

提交回复
热议问题