Processing more than one button click at Android Widget

后端 未结 1 374
北恋
北恋 2020-11-29 00:42

I saw this topic and implement IntentService as describes, but what if I want more that one button? How can I distinguish button from each other? I\'m trying to setFlags, bu

相关标签:
1条回答
  • 2020-11-29 01:22

    That works, out Widget class that extends AppWidgetProvider:

    public class ExampleProvider extends AppWidgetProvider {
    
    // our actions for our buttons
    public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh";
    public static String ACTION_WIDGET_SETTINGS = "ActionReceiverSettings";
    public static String ACTION_WIDGET_ABOUT = "ActionReceiverAbout";
    
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main_layout);
    
        Intent active = new Intent(context, ExampleProvider.class);
        active.setAction(ACTION_WIDGET_REFRESH);
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        remoteViews.setOnClickPendingIntent(R.id.button_refresh, actionPendingIntent);
    
        active = new Intent(context, ExampleProvider.class);
        active.setAction(ACTION_WIDGET_SETTINGS);
        actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        remoteViews.setOnClickPendingIntent(R.id.button_settings, actionPendingIntent);
    
        active = new Intent(context, ExampleProvider.class);
        active.setAction(ACTION_WIDGET_ABOUT);
        actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        remoteViews.setOnClickPendingIntent(R.id.button_about, actionPendingIntent);
    
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {
            Log.i("onReceive", ACTION_WIDGET_REFRESH);
        } else if (intent.getAction().equals(ACTION_WIDGET_SETTINGS)) {
            Log.i("onReceive", ACTION_WIDGET_SETTINGS);
        } else if (intent.getAction().equals(ACTION_WIDGET_ABOUT)) {
            Log.i("onReceive", ACTION_WIDGET_ABOUT);
        } else {
            super.onReceive(context, intent);
        }
    }
    ...
    

    And AndroidManifest.xml where we must register our actions:

        <receiver android:name="ExampleProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
                <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_REFRESH"/>
                <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_SETTINGS"/>
                <action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_ABOUT"/>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"/>
        </receiver>
    </application>
    

    0 讨论(0)
提交回复
热议问题