1. 程式人生 > >移動開發----啟動到三方軟體,百度 高德(路線)

移動開發----啟動到三方軟體,百度 高德(路線)

//點選跳轉到百度   或者  高德 地圖   
 public void ding(View v) {
        new AlertDialog.Builder(this)
                .setCancelable(true)
                .setItems(new String[]{"百度地圖", "高德地圖"}, new DialogInterface.OnClickListener() {


                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == 0) {
                            if (isAvilible(MapAddressActivity.this, "com.baidu.BaiduMap")) {//傳入指定應用包名

                                try {
                                   Intent intent = Intent.getIntent("intent://map/marker?location="+doubles[0]+","+doubles[1]+"&title="+name+"&content="+name+"&src=e群|#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                                    startActivity(intent); //啟動呼叫
                                } catch (URISyntaxException e) {
                                    Log.e("intent", e.getMessage());
                                }
                            } else {//未安裝
                                //market為路徑,id為包名
                                //顯示手機上所有的market商店
                                Toast.makeText(MapAddressActivity.this, "您尚未安裝百度地圖", Toast.LENGTH_LONG).show();
                                Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
                                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                startActivity(intent);
                            }
                        } else {

                            if (isAvilible(MapAddressActivity.this, "com.autonavi.minimap")) {
                                try{
                                    Intent intent = Intent.getIntent("androidamap://viewMap?sourceApplication=e群&poiname="+name+"&lat="+longitude1 +"&lon="+latitude1+"&dev=0");
                                    startActivity(intent);
                                } catch (Exception e)
                                {e.printStackTrace(); }
                            }else{
                                Toast.makeText(MapAddressActivity.this, "您尚未安裝高德地圖", Toast.LENGTH_LONG).show();
                                Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
                                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                startActivity(intent);
                            }
                        }
                    }
                }).show();
    }
    //    * 檢查手機上是否安裝了指定的軟體
//    * @param context
//    * @param packageName:應用包名
//    * @return
//            */
    public static boolean isAvilible(Context context, String packageName) {
        //獲取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //獲取所有已安裝程式的包資訊
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用於儲存所有已安裝程式的包名
        List<String> packageNames = new ArrayList<String>();
        //從pinfo中將包名字逐一取出,壓入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判斷packageNames中是否有目標程式的包名,有TRUE,沒有FALSE
        return packageNames.contains(packageName);
    }