2016-08-24 62 views
0

这工作得很好,启动谷歌播放音乐(虽然古怪它给我的“播放音乐”或“谷歌播放音乐”中选择)时:无活动处理的意图,开始谷歌地图

  Intent musicIntent = new Intent(Intent.ACTION_MAIN); 
      musicIntent.setPackage("com.google.android.music"); 
      musicIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(musicIntent); 

Google地图的相同内容不起作用。这:

  Intent mapIntent = new Intent(Intent.ACTION_MAIN); 
      mapIntent.setPackage("com.google.android.apps.maps"); 
      mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(mapIntent); 

给出:

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) 

我仔细检查过包的名称。我只能猜测这不是我应该使用的字符串。我究竟做错了什么?

+0

安装该设备上的谷歌地图? – Blackbelt

回答

3

虽然古怪它给我的“选择播放音乐“或”Google Play音乐“

应用程序欢迎有多个ACTION_MAIN活动。例如,它可能有多个启动器活动,或者它可能会有一个专门的Android启动器活动,以便与标准启动器一起使用。

我在做什么错?

您的意思是,除了假设用户安装了Google地图,Google地图是否启用并希望使用Google地图,还是使用其他地图应用程序?

您正在寻找ACTION_MAINCATEGORY_DEFAULT,在com.google.android.apps.mapsCATEGORY_DEFAULT不需要任何应用程序ACTION_MAIN

你的CATEGORY_DEFAULT选择是通过创建一个Intent,不与任何类别相关联,并使用它与startActivity()暗示。您可能希望使用CATEGORY_LAUNCHER。或者,use getLaunchIntentForPackage() on PackageManager

+0

啊我明白了。很显然,我还没有完成开发我的应用程序 - 我不认为我说这是生产代码。 – Timmmm

1

它不与地图相同。

从谷歌地图文件,他们说,

所有的谷歌地图的意图被称为视图行动 - ACTION_VIEW。

尝试以下方式:

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 
    } 
} 

也请您查看的Common Intents for Maps

的文件,意向请求科Google Maps Documentation

1

变化ACTION_MAINACTION_VIEW

Intent mapIntent = new Intent(Intent.ACTION_VIEW); 
mapIntent.setPackage("com.google.android.apps.maps"); 
mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(mapIntent);