处理多个按钮点击在Android部件多个、部件、按钮、Android

2023-09-12 01:34:33 作者:-苦笑 附和你的敷衍ヽ

我看到这个主题并实施IntentService作为描述,但如果我想更多的是一个按钮?我怎样才能区分彼此按钮? 我想setFlags,但不能在onHandleIntent阅读()方法:

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, but cannot read it at onHandleIntent() method:

public static class UpdateService extends IntentService {
...

    @Override
    public void onHandleIntent(Intent intent) {
        ComponentName me = new ComponentName(this, ExampleProvider.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(me, buildUpdate(this));
    }

    private RemoteViews buildUpdate(Context context) {
        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.main_layout);

        Intent i = new Intent(this, ExampleProvider.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        updateViews.setOnClickPendingIntent(R.id.button_refresh, pi);

        i = new Intent(this, ExampleProvider.class); 
        pi = PendingIntent.getBroadcast(context, 0, i, 0);
        updateViews.setOnClickPendingIntent(R.id.button_about, pi);

        return updateViews;
    }
}

目前小片code我有setOnClickPendingIntent 2 PendingIntent联系,我可以分辨这种意图不同的动作和处理? 感谢您的帮助

At this little piece of code I have two PendingIntent linked with setOnClickPendingIntent, can I distinguish this intent for different actions and processing? Thanks for help

推荐答案

这样的作品,从Widget类,扩展AppWidgetProvider:

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);
    }
}
...

和AndroidManifest.xml中,我们必须注册我们的行动:          

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>