Navigation apps with IntentChooser- it's the s

2019-04-11 12:52发布

I try to invoke the navigation from my applicatoin.

I call with this query:

String link = "geo:" + posInfo.getLatitude() + "," + posInfo.getLongitude();
Intent navigateIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
Intent chooser = Intent.createChooser(navigateIntent, ctx.getString(R.string.navigate_intent_chooser_title));
ctx.startActivity(chooser);

The intent chooser displays 2 apps: GoogleMaps and Waze.

The navigation with Waze works perfectly but with Google maps it doesn't work. I get the map displayed but no navigation instructions. If I use GoogleMaps directly then I can navigate so it is something to do with how the intent parameters are passed.

I read in some other answers that I need to use another query for google maps:

"google.navigation:q.."

I have 2 questions:

  1. Do I need different query to different navigation apps?
  2. If 1 is true, how can I use intentChooser with different query?

1条回答
小情绪 Triste *
2楼-- · 2019-04-11 13:14

It's my solution:

private static final String PERFIX_DEFAULT_NAVIGATE_LINK_BY_COORDINATES = "geo:";
private static final String PERFIX_DEFAULT_NAVIGATE_LINK_BY_ADDRESS = "geo:0,0?q=";
private static final String PERFIX_DEFAULT_GOOGLE_NAVIGATE = "google.navigation:q=";

private static Intent getChooserNavigateIntent(Context context, Intent navigateIntent, POSEntityInfo posInfo, boolean isCoordinates) {

    // Check if there is a default app opener for this type of content.
    final PackageManager packageManager = context.getPackageManager();
    ResolveInfo defaultAppInfo = packageManager.resolveActivity(navigateIntent,
            PackageManager.MATCH_DEFAULT_ONLY);


    // create the intent for intent chooser
    List<Intent> targetedOpenIntents = new ArrayList<Intent>();
    List<ResolveInfo> appInfoList = packageManager.queryIntentActivities(navigateIntent,
            PackageManager.MATCH_DEFAULT_ONLY);

    for (ResolveInfo appInfo : appInfoList) {
        String packageName = appInfo.activityInfo.packageName;
        Intent targetedOpenIntent = new Intent(android.content.Intent.ACTION_VIEW).setPackage(packageName);
        targetedOpenIntent = getNavigateIntent(appInfo, targetedOpenIntent, isCoordinates, posInfo);
        targetedOpenIntents.add(targetedOpenIntent);
    }

    // create the intent chooser. delete the first member in the list(the default activity)
    Intent chooserIntent = Intent.createChooser(targetedOpenIntents.remove(0),
            context.getString(R.string.navigate_intent_chooser_title)).putExtra(Intent.EXTRA_INITIAL_INTENTS,
            targetedOpenIntents.toArray(new Parcelable[]{}));

    return chooserIntent;
}

private static Intent getNavigateIntent(ResolveInfo appInfo, Intent navigateIntent, boolean isCoordinates, POSEntityInfo posInfo) {
    // link urls
    final String GOOGLE_NAVIGATE_PACKAGE = "com.google.android.apps.maps";
    String suffixAddressLink = posInfo.getStreet() + " " + posInfo.getStreetNum() + "," + posInfo.getCity();
    String suffixCoordinatesLink = posInfo.getLatitude() + "," + posInfo.getLongitude();

    boolean isGoogleMaps = GOOGLE_NAVIGATE_PACKAGE.equals(appInfo.activityInfo.packageName);

    //build the link url
    StringBuilder link = new StringBuilder();
    link.append(isCoordinates ? (isGoogleMaps ? PERFIX_DEFAULT_GOOGLE_NAVIGATE : PERFIX_DEFAULT_NAVIGATE_LINK_BY_COORDINATES) : (isGoogleMaps ? PERFIX_DEFAULT_GOOGLE_NAVIGATE : PERFIX_DEFAULT_NAVIGATE_LINK_BY_ADDRESS));
    link.append(isCoordinates ? suffixCoordinatesLink : suffixAddressLink);

    Log.d(TAG, "Link = " + link);

    return navigateIntent.setData(Uri.parse(link.toString()));
}

//Just closed the tags with >

查看更多
登录 后发表回答