【问题标题】:Android intent chooser - navigation appsAndroid 意图选择器 - 导航应用
【发布时间】:2019-09-03 14:51:47
【问题描述】:

在我的应用中,我有一个按钮,可以打开带有已发送坐标的 Google 地图。

 final ImageView view = findViewById(R.id.navigate);
        view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                        Uri.parse("http://maps.google.com/maps?daddr="+gps1+","+gps2));
                startActivity(intent);
            }
        });

这工作正常,但我想在点击按钮后向用户提供他设备中的所有导航应用程序。

所以不要自动打开谷歌地图,而是让用户选择使用哪个应用。

我找到了this solution,但这仅适用于 Google 地图和 Waze。由于我不知道用户的设备上有哪些导航应用以及有多少导航应用,因此我无法对所有现有导航应用进行硬编码。

String url = "waze://?ll=" + latitude + ", " + longitude + "&navigate=yes";
Intent intentWaze.setPackage("com.waze");
Intent intentWaze = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

String uriGoogle = "google.navigation:q=" + latitude + "," + longitude;
Intent intentGoogleNav.setPackage("com.google.android.apps.maps");
Intent intentGoogleNav = new Intent(Intent.ACTION_VIEW, Uri.parse(uriGoogle));

String title = context.getString(R.string.title);
Intent chooserIntent = Intent.createChooser(intentGoogleNav, title);
Intent[] arr = new Intent[1];
arr[0] = intentWaze;
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arr);
context.startActivity(chooserIntent);

那么有什么解决方案可以提供用户拥有的所有导航应用程序吗?

例如分享功能是这样的:

 Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT,
                        infox);
                sendIntent.setType("text/plain");
                startActivity(Intent.createChooser(sendIntent, getString(R.string.share)));

这将显示许多具有共享可能性的应用程序。如何为带有地图的应用程序做同样的事情?

编辑:我将此添加到 google 和 waze 代码中,所以现在我已在选择器中列出:Google 地图、Waze 和一个未知的 android 图标,当我点击它时,它会显示我设备上的所有导航应用程序!但是,只有当我单击那个未命名的图标并且它只会打开那些没有导航的地图。

 String urlx = "geo:" + gps1 + ", " + gps2;
                Intent intentOther = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));

我需要在点击按钮后立即显示所有这些应用程序。

【问题讨论】:

  • 你试过geo: common intent吗?
  • woohoo,我之前尝试过 geo,但同时使用了 google 和 waze。现在我只尝试了 geo,如下所示: String urlx = "geo:" + gps1 + ", " + gps2; Intent 意图 = new Intent(Intent.ACTION_VIEW);这真的向我展示了所有的导航应用程序!这很好,但问题是,它只显示地图上的点,而不是导航路径。我对谷歌和 waze 的原始解决方案还显示了导航路径,地理仅显示点(根据坐标)。所以这是一个非常好的方法,只是为了找到导航路线的解决方案

标签: android android-intent


【解决方案1】:

所以我发现,没有通用的方法可以在意图选择器中的所有导航应用程序上强制导航路线,所以我像上面提到的那样解决了它,只是我像这样向 geo 添加了一个参数:

geo:0,0?q=latitude,longitude

并添加到清单中:

  <data android:scheme="geo"/>

这会在选择器中显示我设备上的所有导航应用,并且主要在地图上显示带有坐标的点,但是像 Waze 这样的一些应用也会立即计算路线,所以我认为这是一个很好的解决方案。

【讨论】:

    【解决方案2】:

    这是一年后的事,我实际上是在尝试使用上面的解决方案,但遇到了一些问题。比如选择器中的两个位智应用

    我刚刚重写了 kotlin 中的解决方案以及对我真正有用的解决方案

      val url = "waze://?ll=" + latitude + ", " + longitude + "&navigate=yes"
      var intentWaze = Intent(Intent.ACTION_VIEW, Uri.parse(url)).setPackage("com.waze")
    
      val uriGoogle = "google.navigation:q=" + latitude + "," + longitude
      var intentGoogleNav = Intent(Intent.ACTION_VIEW, Uri.parse(uriGoogle)).setPackage("com.google.android.apps.maps")
    
      val title: String = context.getString(R.string.app_name)
      val chooserIntent = Intent.createChooser(intentGoogleNav, title)
      val arr = arrayOfNulls<Intent>(1)
      arr[0] = intentWaze
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arr)
      context.startActivity(chooserIntent)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2011-05-24
      • 1970-01-01
      • 2021-05-31
      • 2011-10-17
      • 1970-01-01
      相关资源
      最近更新 更多