android intent to open google maps in android 11 not working
Asked Answered
N

3

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

Newton answered 31/8, 2021 at 2:18 Comment(2)
You need to add query in the manifestChemotherapy
For anyone else struggling with permissions/manifest there is an alternate open to launch maps by URL. See Google articleAssess
C
16

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

Chemotherapy answered 31/8, 2021 at 2:25 Comment(1)
How can I add this on a Cordova/Ionic project that doesn't have a <manifest> that I can directly edit? There is <platform name="android"> and <edit-config> elements in my config.xml but adding the above <queries> element within the <edit-config> element did not work on that fileAssess
I
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
Ilonailonka answered 31/8, 2021 at 2:23 Comment(2)
Thanks for the answer I saw this answer here https://mcmap.net/q/407924/-intent-action_dial-does-not-function-in-android-11 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 thanksNewton
@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.Tarsia
F
-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>
Fredra answered 6/9, 2021 at 22:51 Comment(1)
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 exclusionsYahoo

© 2022 - 2024 — McMap. All rights reserved.