本文主要是记录一些零碎的东西
判断是否安装某个应用,提供两种方法,以微信为例,都可以,但是推荐第二种,假如用户装了特别特别多应用呢
/**
* 判断微信是否安装
* @param context
* @return
*/
public static boolean isWeixinAvilible(Context context){
// return isApplicationAvilible(context,"com.tencent.mm");
return isClientInstalled(context,"com.tencent.mm");
}
// 遍历所有的安装包
public static boolean isApplicationAvilible(Context context,String appPackageName) {
PackageManager packageManager = context.getPackageManager();// 获取packagemanager
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName;
D.i("slack",pn);
if (appPackageName.equals(pn)) {
return true;
}
}
}
return false;
}
// 使用Intent queryIntentActivities 判断
public static boolean isClientInstalled(Context context,String appPackageName) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage(appPackageName);
i.setType("image/*");
PackageManager pm = context.getPackageManager();
List<?> ris = pm.queryIntentActivities(i, PackageManager.GET_ACTIVITIES);
return ris != null && ris.size() > 0;
}