private boolean isAvilible(Context context, String packageName){
final PackageManager packageManager = context.getPackageManager();//获取packagemanager
List< packageInfo> pinfo = packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息
List<String> pName = new ArrayList<String>();//用于存储所有已安装程序的包名
//从pinfo中将包名字逐一取出,压入pName list中
if(pinfo != null){
for(int i = 0; i < pinfo.size(); i++){
String pn = pinfo.get(i).packageName;
pName.add(pn);
}
}
return pName.contains(packageName);//判断pName中是否有目标程序的包名,有TRUE,没有FALSE
}
判断后的逻辑:
//已安装,打开程序,需传入参数包名:"com.skype.android.verizon"
if(isAvilible(this, "com.skype.android.verizon")){
Intent i = new Intent();
ComponentName cn = new ComponentName("com.skype.android.verizon",
"com.skype.android.verizon.SkypeActivity");
i.setComponent(cn);
startActivityForResult(i, RESULT_OK);
}
//未安装,跳转至market下载该程序
else {
Uri uri = Uri.parse("market://details?id=com.skype.android.verizon");//id为包名
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
}