判断手机已安装某程序的方法:
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.tencent.mm" if(isAvilible(this, "com.skype.android.verizon")){ Intent i = new Intent(); ComponentName cn = new ComponentName("com.tencent.mm", "com.tencent.mm.WeiXinActivity"); i.setComponent(cn); startActivityForResult(i, RESULT_OK); } //未安装,跳转至market下载该程序 else { Uri uri = Uri.parse("market://details?id=com.tencent.mm");//id为包名
// 直接从指定网址下载
// Uri uri = Uri.parse("http://dldir1.qq.com/foxmail/weixin521android400.apk");
Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); }