如何判断Intent.createChooserselect了哪个应用程序?

码:

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

我看不出有什么办法可以说明select了哪种Intent(HOME类别的启动器)。 没有Inent.addOnActionSetListener,并没有chooser.addOnIntentChosenListener等。那么我怎么能告诉哪个被选中? 做还是必须为此编写我自己的select器?

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

在Android 5.1之前,Android没有任何东西让你知道用户select了什么。

这应该适用于Android的早期版本。

使用意向select器而不是CHOOSER。 不同之处在于,select器不会自动启动目标意图,而是返回onActivityResult()目标意图,并附加所选应用程序的组件名称。 然后,您在callback中启动目标意图作为第二步。

一点代码应该解释一下,

 // 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提供的答案有效,但问题是显示了大量的应用程序。 在这个解决scheme中,我使用Intent.ACTION_PICK_ACTIVITY,但只会显示与Intent.ACTION_SEND兼容的应用程序,并且您将知道用户select了哪个选项。

 public void doSocialShare(String title, String text, String url){ // First search for compatible apps with sharing (Intent.ACTION_SEND) List<Intent> targetedShareIntents = new ArrayList<Intent>(); 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<ResolveInfo> 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); } } 

最后,为了能够获得用户select的应用程序,您必须在您的活动上覆盖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); } } }