如何判断Intent.createChooser选择了哪个应用?

码:

Intent launchIntent = new Intent(Intent.ACTION_MAIN); launchIntent.addCategory(Intent.CATEGORY_HOME); Intent chooser = Intent.createChooser(launchIntent, "Complete Action using.."); activity.startActivity(chooser); 

我没有看到任何方法来判断选择了哪个Intent(HOME类别启动器)。 没有Inent.addOnActionSetListener,也没有chooser.addOnIntentChosenListener等。那么如何判断哪个被选中? 或者我必须为此编写自己的选择器吗?

Solutions Collecting From Web of "如何判断Intent.createChooser选择了哪个应用?"

在Android 5.1+上,您可以使用createChooser()方法的三参数版本 ,其中最后一个参数是一个IntentSender ,您可以使用它来查找所选内容。

在Android 5.1之前,Android中没有任何内容可以让您了解用户选择的内容。

这适用于早期版本的Android。

使用intent PICKER而不是CHOOSER。 区别在于选择器不会自动启动目标意图,而是返回onActivityResult()目标意图,并附加所选应用程序的组件名称。 然后,作为第2步,在回调中启动目标意图。

一点代码应该解释一下,

 // In MyActivity class static final int REQUEST_CODE_MY_PICK = 1; // Getting ready to start intent. Note: call startActivityForResult() ... launchIntent = the target intent you want to start; Intent intentPick = new Intent(); intentPick.setAction(Intent.ACTION_PICK_ACTIVITY); intentPick.putExtra(Intent.EXTRA_TITLE, "Launch using"); intentPick.putExtra(Intent.EXTRA_INTENT, launchIntent); this.startActivityForResult(intentPick, REQUEST_CODE_MY_PICK); // You have just started a picker activity, // let's see what user will pick in the following callback @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_MY_PICK) { String appName = data.getComponent().flattenToShortString(); // Now you know the app being picked. // data is a copy of your launchIntent with this important extra info added. // Don't forget to start it! startActivity(data); } } 

BinHe提供的答案有效但问题是显示了大量的应用程序。 在此解决方案中,我使用Intent.ACTION_PICK_ACTIVITY,但只显示与Intent.ACTION_SEND兼容的应用程序,您将知道用户选择了哪个选项。

 public void doSocialShare(String title, String text, String url){ // First search for compatible apps with sharing (Intent.ACTION_SEND) List targetedShareIntents = new ArrayList(); Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("text/plain"); // Set title and text to share when the user selects an option. shareIntent.putExtra(Intent.EXTRA_TITLE, title); shareIntent.putExtra(Intent.EXTRA_TEXT, url); shareIntent.putExtra(Intent.EXTRA_TEXT, text); List resInfo = getPackageManager().queryIntentActivities(shareIntent, 0); if (!resInfo.isEmpty()) { for (ResolveInfo info : resInfo) { Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND); targetedShare.setType("text/plain"); // put here your mime type targetedShare.setPackage(info.activityInfo.packageName.toLowerCase()); targetedShareIntents.add(targetedShare); } // Then show the ACTION_PICK_ACTIVITY to let the user select it Intent intentPick = new Intent(); intentPick.setAction(Intent.ACTION_PICK_ACTIVITY); // Set the title of the dialog intentPick.putExtra(Intent.EXTRA_TITLE, title); intentPick.putExtra(Intent.EXTRA_INTENT, shareIntent); intentPick.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray()); // Call StartActivityForResult so we can get the app name selected by the user this.startActivityForResult(intentPick, REQUEST_CODE_MY_PICK); } } 

最后,为了能够获得用户选择的应用,您必须覆盖活动的onActivityResult:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == REQUEST_CODE_MY_PICK) { if(data != null && data.getComponent() != null && !TextUtils.isEmpty(data.getComponent().flattenToShortString()) ) { String appName = data.getComponent().flattenToShortString(); // Now you know the app being picked. // data is a copy of your launchIntent with this important extra info added. // Start the selected activity startActivity(data); } } } 

我以不同的方式做了, 不需要实现自定义组件

发送意图:

 Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "My feature text"); sendIntent.setType("text/plain"); Intent receiver = new Intent(this, ApplicationSelectorReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT); Intent chooser = Intent.createChooser(sendIntent, null, pendingIntent.getIntentSender()); startActivity(chooser); 

在清单中添加BroadcastReceiver ApplicationSelectorReceiver.class

  

ApplicationSelectorReceiver.java

 public class ApplicationSelectorReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { for (String key : Objects.requireNonNull(intent.getExtras()).keySet()) { try { ComponentName componentInfo = (ComponentName) intent.getExtras().get(key); PackageManager packageManager = context.getPackageManager(); assert componentInfo != null; String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(componentInfo.getPackageName(), PackageManager.GET_META_DATA)); Log.i("Selected Application Name", appName); } catch (Exception e) { e.printStackTrace(); } } } } 

结果:

 Gmail Facebook Hangouts Instagram Drive 

希望这会有助于其他人。