Android 意图选择器 - 导航应用 [英] Android intent chooser - navigation apps

查看:48
本文介绍了Android 意图选择器 - 导航应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my app I have a button, which opens a Google map with sent coordinates.

 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 is working fine, but I want to offer to the user all navigation apps that he has in his device after button click.

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

So not to automatically open Google Maps, but let the user choose which app to use.

我找到了这个解决方案,但这仅适用于 Google 地图和 Waze.由于我不知道用户在其设备上安装了哪些导航应用以及多少导航应用,因此我无法对所有现有导航应用进行硬编码.

I found this solution, but this is only for Google maps and Waze. As I can't know which and how many navigation apps the user has on his device, I can't hardcode all existing navigation apps.

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);

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

So is there any solution, which can offer all navigation apps that the user has?

例如对于共享功能,它的工作方式如下:

For example for sharing function it works like this:

 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)));

这将显示许多具有共享可能性的应用程序.如何对带有地图的应用执行相同操作?

This will show many apps with sharing possibility. How to do the same for apps with map?

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

I added this to google and waze code, so now I have listed in chooser: Google map, Waze and an unknown android icon, when I click it, it shows all navigation apps on my device! However only if I click that unnamed icon and it only opens those maps without navigation.

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

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

I need to show all those app immediately after button click.

推荐答案

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

So as I found out, there is no universal way to force navigation routes on all navigation apps in intent chooser, so I solved it like I mentioned above, just I added a parameter to geo like this:

geo:0,0?q=latitude,longitude

并添加到清单:

  <data android:scheme="geo"/>

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

and this shows all navigation apps on my device in the chooser, and mostly shows the point on the map with the coordinates, but some apps like Waze also calculates the route immediately, so I think this is a good solution.

这篇关于Android 意图选择器 - 导航应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆