代码:
Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_HOME);
Intent chooser = Intent.createChooser(launchIntent, "Complete Action using..");
activity.startActivity(chooser);
我看不出选择了哪种意图(家庭类别启动器)。没有Intent.addOnActionSetListener,也没有chooser.addOnIntentChosenListener等等。
那么我怎么知道哪一个被选中了呢?我必须为这个写我自己的选择吗?
发布于 2015-08-25 12:10:05
在Android 5.1+上,您可以使用 method,其中最后一个参数是一个IntentSender
,您可以使用它来查找所选择的内容。
在Android5.1之前,Android中没有任何东西可以让您知道用户选择了什么。
发布于 2017-04-27 21:20:53
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<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);
}
}
最后,要获得用户选择的应用程序,您必须在您的活动中覆盖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);
}
}
}
发布于 2015-11-12 01:24:02
这应该适用于早期版本的Android。
使用意图选择器而不是选择器。不同之处在于,选择器不会自动启动目标意图,而是返回给onActivityResult()目标意图,并附带所选应用程序的组件名。然后,在回调中启动目标意图,作为第二步。
一点代码就能解释一下,
// 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);
}
}
https://stackoverflow.com/questions/32203230
复制相似问题