处理Android小部件中的多个按钮点击

19

看到了这个话题并按照描述实现了IntentService,但如果我想要多个按钮怎么办?我如何区分每个按钮? 我尝试使用setFlags,但无法在onHandleIntent()方法中读取它:

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

在这段小代码中,我有两个与setOnClickPendingIntent链接的PendingIntent,我能区分不同的操作和处理吗? 感谢您的帮助!

显然,我找到了答案。我们必须按照这里描述的方式注册意图过滤器http://www.helloandroid.com/files/xmaswidget/android_howto-hellowidget.pdf。现在我会尝试并写下结果。 - dive
1个回答

56

这个可以运行,我们的Widget类继承了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,我们必须在其中注册我们的操作:

    <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>

我很感兴趣为什么 setOnClickPendingIntent 在 onUpdate 和 onReceive 中被使用?谢谢。 - surga

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接