方法:判断是否存在某应用。
public 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
}
测试:
public void main() {
if (isAvilible(this, "com.skype.android.verizon")) {
//已安装,打开程序,需传入参数包名:”com.skype.android.verizon”
Intent intent = new Intent();
intent.setClassName(“包名”, “启动类名”);
startActivity(intent);
// startActivityForResult(i, RESULT_OK);
} else {
//未,使安装
Uri uri = Uri.parse("market://details?id=com.skype.android.verizon");//id为包名
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
}
}
转自:http://ruixiazun.blog.163.com/blog/static/906879182013021115923732/
网友评论