Suppressing Google Maps Intent Selection Dialog

后端 未结 3 1220
我寻月下人不归
我寻月下人不归 2020-12-14 04:41

When I use this code

   string url = \"somegooglemapsurl.com\";
   Intent mapLauncherIntent = new Intent(android.content.Intent.ACTION_VIEW,  Uri.parse(url))         


        
3条回答
  •  半阙折子戏
    2020-12-14 05:32

    I haven't found a perfect solution, but this will at least open maps with the correct destination and public transportation pre-selected. Then all the user has to do is hit the directions button.

    It also checks if google maps is installed and prefers to use that if so.

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?daddr=0,0%20(Imaginary%20Place)&dirflg=r"));
    if (isAppInstalled("com.google.android.apps.maps")) {
        intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
    }
    startActivity(intent);
    

     

    // helper function to check if Maps is installed
    private boolean isAppInstalled(String uri) {
        PackageManager pm = getApplicationContext().getPackageManager();
        boolean app_installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed;
    }
    

提交回复
热议问题