[英]How to determine which app the user selects with the default startActivity(intent) chooser?
I am trying to determine which app the user selects when clicking on a directions button in my app. 我试图确定用户在我的应用程序中单击“方向”按钮时选择的应用程序。 I want the "Always" and "Just Once" options of the default chooser but I need to know which app the user is sent to, which is why I find myself creating a custom chooser.
我想要默认选择器的“始终”和“仅一次”选项,但是我需要知道用户被发送到哪个应用程序,这就是为什么我发现自己创建了一个自定义选择器。 I've done research on SO and have come across the following posts:
我已经对SO进行了研究,并且遇到了以下帖子:
Custom chooser to allow me to know what app the user selects: https://stackoverflow.com/a/23494967/3957979 自定义选择器使我知道用户选择了哪个应用程序: https : //stackoverflow.com/a/23494967/3957979
ActionProvider (doesn't work in my case since this is not a MenuItem): https://stackoverflow.com/a/23495696/3957979 ActionProvider(在我的情况下不起作用,因为这不是MenuItem): https ://stackoverflow.com/a/23495696/3957979
Anyone have any idea on how to do this? 有人对如何执行此操作有任何想法吗? Please let me know if there is a way to use the default chooser and still determine the app the user selects.
请让我知道是否可以使用默认选择器,并仍然确定用户选择的应用程序。 Otherwise, please let me know if there is a way to integrate the "Always" and "Just Once" options into a custom chooser.
否则,请让我知道是否可以将“始终”和“仅一次”选项集成到自定义选择器中。
Below is the code I am implementing: 下面是我要实现的代码:
public View onCreateView(...) {
...
shiftActionButtonDirections.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = directions(mLatitude, mLongitude, location.getFullAddress());
// Check if any apps can handle geo intent
if (intent.resolveActivity(getAppContext().getPackageManager()) != null) {
showChooser(intent, "Get Directions with...");
} else {
Toast.makeText(getAppContext(), R.string.error_maps, Toast.LENGTH_LONG).show();
}
}
});
...
}
public Intent directions(String latitude, String longitude, String address) {
String uri = String.format("geo:%s,%s?q=%s", latitude, longitude, Uri.encode(address));
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return i;
}
private void showChooser(@NonNull final Intent intent, String title) {
final List<ResolveInfo> activities = getAppContext().getPackageManager().queryIntentActivities(intent, 0);
List<String> appNames = new ArrayList<String>();
for (ResolveInfo info : activities) {
appNames.add(info.loadLabel(getAppContext().getPackageManager()).toString());
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(TextUtils.isEmpty(title) ? "With..." : title);
builder.setItems(appNames.toArray(new CharSequence[appNames.size()]), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Map<String, Object> properties = new HashMap<>();
ResolveInfo info = activities.get(item);
if (info.activityInfo.packageName.contains("google")) {
// Google Maps was chosen
} else {
// Another app was chosen
}
// start the selected activity
intent.setPackage(info.activityInfo.packageName);
startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}
Please let me know if there is a way to use the default chooser and still determine the app the user selects.
请让我知道是否可以使用默认选择器,并仍然确定用户选择的应用程序。
Set your minSdkVersion
to 22, then use the three-parameter flavor of createChooser()
. 将您的
minSdkVersion
设置为22,然后使用createChooser()
的三个参数 。 Note that this does not allow you to change anything about the user's choice (eg, substitute a different Intent
); 注意,这不允许您更改有关用户选择的任何内容(例如,替换为其他
Intent
); it merely gives you a chance to find out the choice. 它只是让您有机会找出选择。
Or, if you do not want to set your minSdkVersion
to 22, use the three-parameter flavor of createChooser()
on API Level 22+ devices, and do something else on older ones (custom chooser, live without the choice information, etc.). 或者,如果您不想将
minSdkVersion
设置为22,请在API Level 22+设备上使用createChooser()
的三参数形式,并在较旧的设备上执行其他操作(自定义选择器,不带选择信息的设备等)。 )。
Otherwise, please let me know if there is a way to integrate the "Always" and "Just Once" options into a custom chooser.
否则,请让我知道是否可以将“始终”和“仅一次”选项集成到自定义选择器中。
No, that is not possible. 不,那是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.