Android检查手机上是否安装了第三方软件的方法------本文以百度地图为例

package com.example.myapi.thirdbaidumap;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.myapi.R;

/**
 * 测试程序跳转到第三方地图,并将数据传递过去。
 * @author tony
 *
 */
public class ThirdBaiduMapTestActivity extends Activity implements OnClickListener{
    private Button btn_test;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thirdbaidumaptest);
        btn_test = (Button)findViewById(R.id.baidu_btn_test);
        btn_test.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.baidu_btn_test:
            //如果已安装,
            
            if(isAvilible(this,"com.baidu.BaiduMap")){//传入指定应用包名
                Intent intent = null;
                try {
//                    intent = Intent.getIntent("intent://map/direction?origin=latlng:34.264642646862,108.95108518068|name:我家&destination=大雁塔&mode=driving®ion=西安&src=yourCompanyName|yourAppName#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                    intent = Intent.getIntent("intent://map/direction?" +
                            "origin=大柏树&" +
                            "destination=江湾镇" +
                            "&mode=driving&" +
                            "region=上海市" +
                            "&src=yourCompanyName|yourAppName#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                    startActivity(intent); //启动调用
                } catch (URISyntaxException e) {
                    Log.e("intent", e.getMessage());
                }
            }else{//未安装
                //market为路径,id为包名
                //显示手机上所有的market商店
                Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
                startActivity(intent); 
            }
            break;
        }
    }
    /**
     * 检查手机上是否安装了指定的软件
     * @param context
     * @param packageName:应用包名
     * @return
     */
    private 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);
  } 

}

你可能感兴趣的:(Android检查手机上是否安装了第三方软件的方法------本文以百度地图为例)