找IntentSender对象在Android中createChooser方法 [英] Get IntentSender object for createChooser method in Android

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

问题描述

我想用的<新版本href="http://developer.android.com/reference/android/content/Intent.html#createChooser(android.content.Intent,%20java.lang.CharSequence,%20android.content.IntentSender)">Intent.createChooser方法,它使用 IntentSender

文件只是说我可以从 PendingIntent 实例抓住它。在我而言似乎 PendingIntent 不会有任何其他用途。

有另一种方式来获得 IntentSender 或者我需要创建 PendingIntent

解决方案

选择器目标的意图并非 PendingIntent 。例如,在下面的代码片段,我声明意图 ACTION_SEND ,类型为文本/纯,这是在我的目标意图为 Intent.createChooser 。然后我创建另一个意图,接收和处理程序,在 PendingIntet ,它会调用的onReceive 我的 BroadcastTest 后的一个挑些从选择器。

 意向意图=新的意图(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT,这是我的文字进行发送。);
intent.setType(text / plain的);
意图接收器=新的意图(这一点,BroadcastTest.class);
receiver.putExtra(测试,测试);
PendingIntent pendingIntent = PendingIntent.getBroadcast(此,0,接收器,PendingIntent.FLAG_UPDATE_CURRENT);
意向选择器= Intent.createChooser(意向,测试,pendingIntent.getIntentSender());
startActivity(选配);
 

修改

信息,在的BroadcastReceiver 嵌入你作为参数的意图的情况下。当您选择的选项之一,获取包的额外设备和使用该密钥 android.intent.extra.CHOSEN_COMPONENT ,你应该能够找到用户选取。

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

 的(字符串键:intent.getExtras()键设置()){
    Log.d(。的getClass()getSimpleName(),+ intent.getExtras()获得(键));
}
 

在我的例子中,我得到了

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

电报

<$p$p><$c$c>ComponentInfo{com.google.android.apps.inbox/com.google.android.apps.bigtop.activities.ComposeMessageActivity}

收件箱

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

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

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

解决方案

the chooser target intent is not the 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 BroadcastTest after one pick something from the chooser.

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
intent.setType("text/plain");
Intent receiver = new Intent(this, BroadcastTest.class);
receiver.putExtra("test", "test");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(intent, "test", pendingIntent.getIntentSender());
startActivity(chooser);

Edit:

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 android.intent.extra.CHOSEN_COMPONENT, you should be able to find what the user picked.

Try adding as simple Log.d to onReceive

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

In my example I got

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

for Telegram and

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

for InBox

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

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