原生实现
private boolean isInstalledApp(Context context, String packageName){
//获取packagemanager
final PackageManager packageManager = context.getPackageManager();
//获取所有已安装程序的包信息
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);
}
}
//判断pName中是否有目标程序的包名,有TRUE,没有FALSE
return pName.contains(packageName);
}
Native.js
var isInstalledApp = function(pkgname) {
plus.android.importClass('java.util.ArrayList');
plus.android.importClass('android.content.pm.PackageInfo');
plus.android.importClass('android.content.pm.PackageManager');
var MainActivity = plus.android.runtimeMainActivity();
var PackageManager = MainActivity.getPackageManager();
var pinfo = plus.android.invoke(PackageManager, 'getInstalledPackages', 0)
if (pinfo != null) {
for (i = 0; i < pinfo.size(); i++) {
//PackageInfo{4b45699f9d com.tencent.mobileqq}
if (pinfo.get(i).toString().indexOf(pkgname) !== -1) {
return true;
}
}
}
return false;
}
## 使用 isInstalledApp('com.tencent.mm')
注意个别手机如华为有权限问题,如果不给权限,无法获取本机应用列表。
方式二:
var getIntentByPackageName = function(pkgname) {
plus.android.importClass('android.content.pm.PackageManager');
var MainActivity = plus.android.runtimeMainActivity();
var PackageManager = MainActivity.getPackageManager();
return plus.android.invoke(PackageManager, 'getLaunchIntentForPackage', pkgname);
}
var isInstalledApp = function(pkgname) {
return getIntentByPackageName(pkgname) != null;
}
## 使用 isInstalledApp('com.tencent.mm')
7 个评论
要回复文章请先登录或注册
bishuihanshan
x***@126.com
7***@qq.com
Element
ZIFeng_
ngkeny
ngkeny