android - 在 Android Widget 上处理多个按钮单击

标签 android widget

saw this topic并按照描述实现 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;
    }
}

在这小段代码中,我有两个 PendingIntentsetOnClickPendingIntent 链接,我可以区分这个 Intent 用于不同的操作和处理吗? 感谢您的帮助!

最佳答案

这行得通,我们的 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>

关于android - 在 Android Widget 上处理多个按钮单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2471875/

相关文章:

java - 在Android NDK中调用C++类中的SO函数

android - 打开带有空格的本地 Android 文件

android - android 小部件中的 TextView 不更新

python - 更改 jupyter notebook 标签小部件中的字体属性

user-interface - Flutter - 如何在 SimpleDialog 框中显示 GridView?

jquery - 将本地数据库与/JQuery Mobile 一起使用?

java - 将 api (rito api) 与 java (android) 一起使用

android - firestore 和 google photos 依赖项冲突

javascript - 将 jqWidgets 组合框限制为值列表

c++ - 如何确定 QAction 的来源?