Reputation: 96546
This works fine, to start Google Play Music (although weirdly it gives me a choice of "Play Music" or "Google Play Music"):
Intent musicIntent = new Intent(Intent.ACTION_MAIN);
musicIntent.setPackage("com.google.android.music");
musicIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(musicIntent);
The same thing for Google Maps doesn't work. This:
Intent mapIntent = new Intent(Intent.ACTION_MAIN);
mapIntent.setPackage("com.google.android.apps.maps");
mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mapIntent);
Gives:
Unhandled exception in callback
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN flg=0x10000000 pkg=com.google.android.apps.maps }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1514)
at android.app.ContextImpl.startActivity(ContextImpl.java:663)
at android.app.ContextImpl.startActivity(ContextImpl.java:645)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
I've double checked the package name. I can only guess that isn't the string I'm supposed to be using. What am I doing wrong?
Upvotes: 0
Views: 3074
Reputation: 25267
It doesnt go same with maps.
From the Google Maps Documentation, they say,
All Google Maps intents are called as a View action — ACTION_VIEW.
Try the below way:
showMap(Uri.parse("geo:47.6,-122.3"););
public void showMap(Uri geoLocation) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(geoLocation);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else{
// No application is available that can handle this intent
}
}
Please check the documentation of Common Intents for Maps
Also, Intent requests Section in Google Maps Documentation
Upvotes: 2
Reputation: 3455
Change ACTION_MAIN
with ACTION_VIEW
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setPackage("com.google.android.apps.maps");
mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mapIntent);
Upvotes: 1
Reputation: 1006614
although weirdly it gives me a choice of "Play Music" or "Google Play Music"
An app is welcome to have multiple ACTION_MAIN
activities. For example, it might have multiple launcher activities, or it might have a dedicated launcher activity for Android TV to go along with a standard launcher activity.
What am I doing wrong?
You mean, besides assuming that the user has Google Maps installed, has Google maps enabled, and wants to use Google Maps, versus using some other map application?
You are looking for ACTION_MAIN
, CATEGORY_DEFAULT
, in com.google.android.apps.maps
. There is no requirement for any app to have ACTION_MAIN
for CATEGORY_DEFAULT
.
Your choice of CATEGORY_DEFAULT
is implied by creating an Intent
, not associating it with any category, and using it with startActivity()
. You might wish to use CATEGORY_LAUNCHER
. Or, use getLaunchIntentForPackage()
on PackageManager
.
Upvotes: 3