Travis Yim
Travis Yim

Reputation: 404

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:

Custom chooser to allow me to know what app the user selects: 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

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();
}

Upvotes: 0

Views: 1225

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

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(). Note that this does not allow you to change anything about the user's choice (e.g., substitute a different 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.).

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.

Upvotes: 2

Related Questions