在 Android Widget 上处理多个按钮单击 [英] Processing more than one button click at Android Widget

查看:16
本文介绍了在 Android Widget 上处理多个按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到了这个话题并实现了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;
    }
}

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

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

推荐答案

这行,out Widget class that extends 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>

这篇关于在 Android Widget 上处理多个按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆