小工具发送多种类型的意图[英] Widget sending multiple types of intents

本文是小编为大家收集整理的关于小工具发送多种类型的意图的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在构建一个具有多个按钮的小部件,每个小部件都将其自行发出到广播接收器.广播接收器假设显示基于哪个按钮被推动的吐司消息.该代码当前如下所示:

public class WidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){

        ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        for (int widgetId : allWidgetIds) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

            // Set the text of the buttons
            remoteViews.setTextViewText(R.id.widgetPreset1Button, prefs.getString("widget1", "Not set"));
            remoteViews.setTextViewText(R.id.widgetPreset2Button, prefs.getString("widget2", "Not set"));
            remoteViews.setTextViewText(R.id.widgetPreset3Button, prefs.getString("widget3", "Not set"));
            remoteViews.setTextViewText(R.id.widgetPreset4Button, prefs.getString("widget4", "Not set"));

            // Register the buttons with an OnClick event
            Intent intent1 = new Intent("myapp.SendWidgetPreset");
            intent1.putExtra("Widget", 1);
            PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset1Button, pendingIntent1);

            Intent intent2 = new Intent("myapp.SendWidgetPreset");
            intent2.putExtra("Widget", 2);
            PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset2Button, pendingIntent2);

            Intent intent3 = new Intent("myapp.SendWidgetPreset");
            intent3.putExtra("Widget", 3);
            PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, 0, intent3, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset3Button, pendingIntent3);

            Intent intent4 = new Intent("myapp.SendWidgetPreset");
            intent4.putExtra("Widget", 4);
            PendingIntent pendingIntent4 = PendingIntent.getBroadcast(context, 0, intent4, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset4Button, pendingIntent4);

            new WidgetBroadcastReceiver();

            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }
}

和broadcastreceiver:

public class WidgetBroadcastReceiver extends BroadcastReceiver{

    public WidgetBroadcastReceiver(){
    }

    @Override
    public void onReceive(Context context, Intent arg1) {
        int widget = arg1.getIntExtra("Widget", -1);

        Toast.makeText(context, "Widget pressed: " + widget, Toast.LENGTH_SHORT).show();    
    }
}

我的问题是它总是显示Widget pressed: 4,无论按下哪个按钮.如果我放置了四行intent4,intent4.putExtra(),pendingIntent4和remoteViews.setOnClickPendingIntent()之上的所有其他意图,那么它将总是说Widget pressed: 3.换句话说,无论最后一个意图注册是什么,它是Toast消息中显示的窗口小部件.

任何人都知道为什么这不起作用我想要它?

推荐答案

您需要为每个PendingTent Ex提供单独的请求代码:

PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT); 
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 1/*request code*/, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

其他推荐答案

您的未来一部分覆盖.这是因为它们比较了被封装的意图,并且在比较意图时不再考虑额外.为每个意图执行此操作:

Intent intent1 = new Intent("myapp.SendWidgetPreset");
intent1.putExtra("Widget", 1);

// This line makes your intents different
intent1.setData(Uri.parse(intent1.toUri(Intent.URI_INTENT_SCHEME)));

PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetPreset1Button, pendingIntent1);

其他推荐答案

似乎是pendingintent.getbroadcast()将忽略请求代码(与pendingintent.pletactivity不同).

因此,要制作唯一的挂件,您可以提供意图的数据.

示例:

public static Intent makeUniqueIntent(String action, int requestCode) {
        Intent intent = new Intent(action);
        intent.setData(Uri.parse("http://"+ String.valueOf(requestCode)));
        return intent;
    }

然后按照常规进行待定的意图,包括COUSTERCODE.

PendingIntent.getBroadcast(ctx, request_code,
                makeUniqueIntent(NotificationReceiver.INTENT, request_code),
                PendingIntent.FLAG_CANCEL_CURRENT);
在意图中的数据元素中,AndroidManifest.xml中的匹配意图过滤器也必须具有数据元素:

<receiver android:name=".service.NotificationReceiver">
       <intent-filter>
           <action android:name="my.package.my_action_string"/>
           <data android:scheme="http"/>
       </intent-filter>
</receiver>

上述意图滤波器仅作为一个方案(即"http")被识别出来.因此,具有该方案的任何URI都将匹配此筛选器的"数据"元素,并且将调用相应的接收器类.

注意:

  • notificationreceiver是我的课程,扩展了Broadcasteceiver
  • notificationReceiver.intent是一个字符串常量,我在NotificationReceiver中声明.在此示例中,它将相等为"my.package.my_action_string";
  • request_code可以是任何东西.使其独特并保存它如果您想在将来的待定意图上引用相同的情况(例如,取消使用它的警报).

INTINT过滤器的数据测试更多信息:

http://developer.android.com/guide/components/Intents-filters.html #dataTest

本文地址:https://www.itbaoku.cn/post/102573.html

问题描述

I am building a widget that has multiple buttons, each one sending off its own intent to a broadcast receiver. The broadcast receiver is suppose to display a Toast message based on which button was pushed. The code currently looks like this:

public class WidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){

        ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        for (int widgetId : allWidgetIds) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

            // Set the text of the buttons
            remoteViews.setTextViewText(R.id.widgetPreset1Button, prefs.getString("widget1", "Not set"));
            remoteViews.setTextViewText(R.id.widgetPreset2Button, prefs.getString("widget2", "Not set"));
            remoteViews.setTextViewText(R.id.widgetPreset3Button, prefs.getString("widget3", "Not set"));
            remoteViews.setTextViewText(R.id.widgetPreset4Button, prefs.getString("widget4", "Not set"));

            // Register the buttons with an OnClick event
            Intent intent1 = new Intent("myapp.SendWidgetPreset");
            intent1.putExtra("Widget", 1);
            PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset1Button, pendingIntent1);

            Intent intent2 = new Intent("myapp.SendWidgetPreset");
            intent2.putExtra("Widget", 2);
            PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset2Button, pendingIntent2);

            Intent intent3 = new Intent("myapp.SendWidgetPreset");
            intent3.putExtra("Widget", 3);
            PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, 0, intent3, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset3Button, pendingIntent3);

            Intent intent4 = new Intent("myapp.SendWidgetPreset");
            intent4.putExtra("Widget", 4);
            PendingIntent pendingIntent4 = PendingIntent.getBroadcast(context, 0, intent4, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset4Button, pendingIntent4);

            new WidgetBroadcastReceiver();

            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }
}

and the BroadcastReceiver:

public class WidgetBroadcastReceiver extends BroadcastReceiver{

    public WidgetBroadcastReceiver(){
    }

    @Override
    public void onReceive(Context context, Intent arg1) {
        int widget = arg1.getIntExtra("Widget", -1);

        Toast.makeText(context, "Widget pressed: " + widget, Toast.LENGTH_SHORT).show();    
    }
}

My problem is it always displays Widget pressed: 4 regardless of which button is pressed. If I put the four lines intent4, intent4.putExtra(), pendingIntent4, and remoteViews.setOnClickPendingIntent() above all of the other intents, it will then always say Widget pressed: 3. In other words, whatever the last intent registration is, that is the widget displayed in the Toast message.

Anyone know why this isn't working how I want it?

推荐答案

you need to provide seperate request code for each pendingintent ex:

PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT); 
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 1/*request code*/, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

其他推荐答案

Your PendingIntents are being overwritten by the next one. THis is because they compare the Intent being encapsulated, and extras are not considered when comparing Intents. Do this for each intent:

Intent intent1 = new Intent("myapp.SendWidgetPreset");
intent1.putExtra("Widget", 1);

// This line makes your intents different
intent1.setData(Uri.parse(intent1.toUri(Intent.URI_INTENT_SCHEME)));

PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetPreset1Button, pendingIntent1);

其他推荐答案

Seems like PendingIntent.getBroadcast() will ignore requestCode (unlike PendingIntent.getActivity).

Thus, to make unique PendingIntents you could supply Data for the Intent.

Example:

public static Intent makeUniqueIntent(String action, int requestCode) {
        Intent intent = new Intent(action);
        intent.setData(Uri.parse("http://"+ String.valueOf(requestCode)));
        return intent;
    }

Then make your Pending Intent as per usual, including the requestCode.

PendingIntent.getBroadcast(ctx, request_code,
                makeUniqueIntent(NotificationReceiver.INTENT, request_code),
                PendingIntent.FLAG_CANCEL_CURRENT);

With a Data element in an Intent, a matching Intent Filter within AndroidManifest.xml must also have a Data element:

<receiver android:name=".service.NotificationReceiver">
       <intent-filter>
           <action android:name="my.package.my_action_string"/>
           <data android:scheme="http"/>
       </intent-filter>
</receiver>

The above intent-filter works as only a scheme (i.e. "http") was identified. So any Uri with that scheme will match this filter's "data" element and the corresponding Receiver class will be called.

Notes:

  • NotificationReceiver is my class, extending BroadcastReceiver
  • NotificationReceiver.INTENT is a String constant I declared in NotificationReceiver. In this example it would equal "my.package.my_action_string";
  • request_code can be anything. Make it unique & save it if you want to reference this same Pending Intent in the future (say for canceling an alarm that uses it).

More info on Data test with Intent Filters:

http://developer.android.com/guide/components/intents-filters.html#DataTest