1,关键代码
//获取一个apk文件的包名,apkFilepath是apk文件的完整路径 PRivate static String getPackageFromAPK(Context context,String apkFilepath) { try { Log.i(tag, "获取一个apk文件的包名 "+apkFilepath); PackageManager packageManager = context.getPackageManager(); PackageInfo info =null; info =packageManager.getPackageArchiveInfo(apkFilepath, PackageManager.GET_ACTIVITIES); if(info == null){ info =packageManager.getPackageArchiveInfo(apkFilepath, PackageManager.GET_SERVICES); } if(info == null){ info = packageManager.getPackageArchiveInfo(apkFilepath, 0); } if(info == null){ return null; }else { applicationInfo appInfo = null; appInfo = info.applicationInfo; String packageName = appInfo.packageName; Log.i( tag, "getPackageFromAPK APKpath:"+apkFilepath+";packageName:"+packageName); if(packageName!=null&&packageName.length()>0){ return packageName; } } } catch (Exception e) { e.printStackTrace(); } //TODO 如果获取包名失败怎么办嘛 return null; }2,是否需要修改权限 apk文件从asset拷贝到data/data/your.packages.names/files/apkfilename.apk下;不需要修改apk的权限就可以获取对应的包名(ls -l查看的时候发现权限为-rw——-); 但是如果要调用系统安装程序来安装这个apk,那么需要对应的权限为允许安装,简单点就是chomd 755
新闻热点
疑难解答