Reputation: 339
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
Upvotes: 8
Views: 6491
Reputation: 719
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>
Upvotes: 0
Reputation: 2421
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:
Upvotes: 5
Reputation: 3963
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
Upvotes: 16