为 Android 中的 createChooser 方法获取 IntentSender 对象 [英] Get IntentSender object for createChooser method in Android

查看:28
本文介绍了为 Android 中的 createChooser 方法获取 IntentSender 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用新版本的 Intent.createChooser 使用 IntentSender 的方法.

I would like to use new version of Intent.createChooser method which uses IntentSender.

文档仅说明我可以从 PendingIntent 实例中获取它.就我而言,PendingIntent 似乎没有任何其他用途.

Documentation states only that I can grab it from PendingIntent instance. In my case it seems that PendingIntent won't have any other use.

是否有其他方法可以获取IntentSender 或者我需要创建PendingIntent?

Is there another way to obtain IntentSender or do I need create PendingIntent?

推荐答案

选择器目标意图不是 PendingIntent.例如,在以下代码段中,我声明了 ACTION_SEND 的意图,类型为 text/plain,这是我对 Intent.createChooser 的目标意图.然后我创建了另一个 Intent、接收器和一个处理程序,PendingIntet,它将在用户从中选择一些东西后调用我的 BroadcastReceiver 的 onReceive选择器.

The chooser target intent is not a PendingIntent. For instance, in the following snippet, I am declaring intent for ACTION_SEND, with type text/plain, and this is the my target intent for the Intent.createChooser. Then I am creating another Intent, receiver, and a handler, the PendingIntet, which will invoke onReceive of my BroadcastReceiver after the user picks something from the chooser.

// Set up the broadcast receiver (preferably as a class member)
BroadcastReceiver receiver = new BroadcastReceiver() {
     @Override
     public void onReceive(Context context, Intent intent) {
         // Unregister self right away
         context.unregisterReceiver(this);

         // Component will hold the package info of the app the user chose
         ComponentName component = intent.getParcelableExtra<ComponentName>(Intent.EXTRA_CHOSEN_COMPONENT);
         // Do something with component
     }
}


Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
intent.setType("text/plain");

// Use custom action only for your app to receive the broadcast
final String shareAction = "com.yourdomain.share.SHARE_ACTION";
Intent receiver = new Intent(shareAction);
receiver.putExtra("test", "test");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(intent, "test", pendingIntent.getIntentSender());

// Before firing chooser activity, register the receiver with our custom action from above so that we receive the chosen app
context.registerReceiver(receiver, new IntentFilter(SHARE_ACTION));

startActivity(chooser);

编辑:

BroadcastReceiver 的情况下,信息嵌入在您作为参数获得的意图中.选择其中一个选项后,检索 Bundle 的附加内容并使用 Intent.EXTRA_CHOSEN_COMPONENT 键,您应该能够找到用户选择的内容.

The information, in the case of the BroadcastReceiver is embedded in the intent you get as parameter. After you selected one of the option, retrieve the Bundle's extras and using the key Intent.EXTRA_CHOSEN_COMPONENT, you should be able to find what the user picked.

尝试将简单的 Log.d 添加到 onReceive

Try adding as simple Log.d to onReceive

for (String key : intent.getExtras().keySet()) {
    Log.d(getClass().getSimpleName(), " " + intent.getExtras().get(key));
}

在我的例子中我得到了

ComponentInfo{org.telegram.messenger/org.telegram.ui.LaunchActivity}

对于Telegram

ComponentInfo{com.google.android.apps.inbox/com.google.android.apps.bigtop.activities.ComposeMessageActivity}

对于 InBox

这篇关于为 Android 中的 createChooser 方法获取 IntentSender 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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