我想使用使用IntentSender
的新版Intent.createChooser方法。
文档仅说明我可以从PendingIntent
实例获取它。在我的情况下,PendingIntent
似乎没有任何其他用途。
是否有另一种获取IntentSender
的方法或我是否需要创建PendingIntent
?
答案 0 :(得分:46)
选择器目标意图不是PendingIntent
。例如,在以下代码段中,我声明ACTION_SEND
的意图,类型为text/plain
,这是我Intent.createChooser
的目标意图。然后我创建了另一个Intent
接收器和一个处理程序PendingIntet
,它将从选择器中选择一个内容后调用onReceive
BroadcastTest
。
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);
修改:
在BroadcastReceiver
的情况下,信息嵌入在您作为参数获得的意图中。选择其中一个选项后,检索Bundle的附加内容并使用密钥android.intent.extra.CHOSEN_COMPONENT
,您应该能够找到用户选择的内容。
尝试将 Log.d 添加到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
答案 1 :(得分:0)
另一种方法。
/**
* Receiver to record the chosen component when sharing an Intent.
*/
static class TargetChosenReceiver extends BroadcastReceiver {
private static final String EXTRA_RECEIVER_TOKEN = "receiver_token";
private static final Object LOCK = new Object();
private static String sTargetChosenReceiveAction;
private static TargetChosenReceiver sLastRegisteredReceiver;
static boolean isSupported() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
static void sendChooserIntent(Activity activity, Intent sharingIntent) {
synchronized (LOCK) {
if (sTargetChosenReceiveAction == null) {
sTargetChosenReceiveAction = activity.getPackageName() + "/"
+ TargetChosenReceiver.class.getName() + "_ACTION";
}
Context context = activity.getApplicationContext();
if (sLastRegisteredReceiver != null) {
context.unregisterReceiver(sLastRegisteredReceiver);
}
sLastRegisteredReceiver = new TargetChosenReceiver();
context.registerReceiver(
sLastRegisteredReceiver, new IntentFilter(sTargetChosenReceiveAction));
}
Intent intent = new Intent(sTargetChosenReceiveAction);
intent.setPackage(activity.getPackageName());
intent.putExtra(EXTRA_RECEIVER_TOKEN, sLastRegisteredReceiver.hashCode());
final PendingIntent callback = PendingIntent.getBroadcast(activity, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Intent chooserIntent = Intent.createChooser(sharingIntent,
activity.getString(R.string.share_link_chooser_title),
callback.getIntentSender());
activity.startActivity(chooserIntent);
}
@Override
public void onReceive(Context context, Intent intent) {
synchronized (LOCK) {
if (sLastRegisteredReceiver != this) return;
context.getApplicationContext().unregisterReceiver(sLastRegisteredReceiver);
sLastRegisteredReceiver = null;
}
if (!intent.hasExtra(EXTRA_RECEIVER_TOKEN)
|| intent.getIntExtra(EXTRA_RECEIVER_TOKEN, 0) != this.hashCode()) {
return;
}
ComponentName target = intent.getParcelableExtra(Intent.EXTRA_CHOSEN_COMPONENT);
if (target != null) {
setLastShareComponentName(context, target);
}
}
}