9

android intent to open Google Maps in android 11 not working any more but works on lower API

we have this function

Intent mapIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + PICK_LATITUDE + "," + PICK_LONGITUDE ));
                mapIntent.setPackage("com.google.android.apps.maps");
                if (mapIntent.resolveActivity(getPackageManager()) != null) {
                    startActivity(mapIntent);
                }else{
                    Snackbar.make(root, "Google apps is not installed" , Snackbar.LENGTH_SHORT).show();

                }

Android studio shows this suggestion:

Consider adding a declaration to your manifest when calling this method; see https://g.co/dev/packagevisibility for details

Zeone line
  • 205
  • 4
  • 13
  • You need to add query in the manifest – Ridha Rezzag Aug 31 '21 at 02:21
  • For anyone else struggling with permissions/manifest there is an alternate open to launch maps by URL. [See Google article](https://developers.google.com/maps/documentation/urls/get-started#forming-the-url) – Paul G Nov 22 '21 at 20:05

3 Answers3

15

Add this to your manifest

<manifest package="com.example.game">

    <queries>
             <package android:name="com.google.android.apps.maps" />
    </queries>
    ...

</manifest>

If your app targets Android 11 or higher and needs to interact with apps other than the ones that are visible automatically, add the element in your app's manifest file. Within the element, specify the other apps by package name, by intent signature, or by provider authority

More details here Declaring package visibility needs

Ridha Rezzag
  • 3,672
  • 1
  • 34
  • 39
  • 1
    How can I add this on a Cordova/Ionic project that doesn't have a `` that I can directly edit? There is and elements in my config.xml but adding the above element within the element did not work on that file – Paul G Nov 15 '21 at 20:30
5

Your problem lies in the line of code intent.resolveActivity(getPackageManager()). When you call resolveActivity, you will get a warning like this:

Consider adding a declaration to your manifest when calling this method; see https://g.co/dev/packagevisibility for details

Check the document under PackageManager, you will see this note:

Note: If your app targets Android 11 (API level 30) or higher, the methods in this class each return a filtered list of apps. Learn more about how to manage package visibility.

So what does that mean?

In android 11, Google added package visibility policy. Apps now have tighter control over viewing other apps. Your application will not be able to view or access applications outside of your application.

What do you need to do?

All you need to do is add below line of code to AndroidManifest.xml:

<manifest>
    <queries>
        <!-- Specific intents you query for -->
        <package android:name="com.google.android.apps.maps" />
    </queries>
</manifest>

More information:

  1. Package visibility in Android 11
  2. Package visibility filtering on Android
Dương Minh
  • 2,062
  • 1
  • 9
  • 21
  • Thanks for the answer I saw this answer here https://stackoverflow.com/a/68659891/6561930 because I searched before I ask my question, but that answer didn't work for me, so i thought your answer was just a copy and paste, tried Ridha answer it did work, then i saw that your new answer has the correct package and it works also thanks – Zeone line Sep 02 '21 at 00:25
  • 1
    @Zeoneline Because in the case of that question, they use `intent` to open and not set package. In your case, you are sending the item to the package `com.google.android.apps.maps` so you need to replace `intent` with `package`. – Dương Minh Sep 02 '21 at 16:44
-1

If you want to open Google Maps from an url (e.g. https://www.google.com/maps/dir/?api=1&dir_action=navigate&origin=${origin.latitude},${origin.longitude}&destination=${destination.latitude},${destination.longitude})

you have to add the queries tag:

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data  android:scheme="https" />
    </intent>
</queries>
Javier Hinmel
  • 599
  • 6
  • 9
  • if you specify `QUERY_ALL_PACKAGES` then you don't have to specify the `queries` tag. but if you specify `QUERY_ALL_PACKAGES` your app will likely be blocked from Play Store unless you meet certain specific exclusions – Adam Burley Nov 16 '21 at 22:46