清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
开发项目中用到的有关获取apk信息的工具类,在做版本升级的时候一般都需要用到部分方法,做点笔记:
public class AppUtil { /** *根据应用包名(package name)启动应用 *参数分别是上下文context和应用包名:pkgName */ public static boolean startApkByPkgName(Context context, String pkgName) { PackageManager packageManager = context.getPackageManager(); Intent intent = new Intent(); intent = packageManager.getLaunchIntentForPackage(pkgName); if (intent == null) { System.out.println("APP not found!"); return false; } context.startActivity(intent); return true; } /** * 根据apk文件路径得到app信息 */ public static ApplicationInfo getApkInfoByPath(Context context,String absPath) { ApplicationInfo appInfo = null; PackageManager pm = context.getPackageManager(); PackageInfo pkgInfo = pm.getPackageArchiveInfo(absPath,PackageManager.GET_ACTIVITIES); if (pkgInfo != null) { appInfo = pkgInfo.applicationInfo; /* 必须加这两句,不然下面icon获取是default icon而不是应用包的icon */ appInfo.sourceDir = absPath; appInfo.publicSourceDir = absPath; } return appInfo; } /** *根据apk应用包名得到应用信息 */ public static PackageInfo getApkInfoByPkgName(Context context, String pkgName) { PackageInfo pkgInfo = null; PackageManager pm = context.getPackageManager(); try { // 0代表是获取版本信息 pkgInfo = pm.getPackageInfo(pkgName, 0); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return pkgInfo; } /** *根据apk文件路径得到应用信息 */ public static PackageInfo getPkgInfoByPath(Context context, String absPath) { PackageInfo pkgInfo = null; PackageManager pm = context.getPackageManager(); pkgInfo = pm.getPackageArchiveInfo(absPath, PackageManager.GET_ACTIVITIES); return pkgInfo; } /** *根据apk路径直接跳到安装界面 */ public static void installApkByPath(Context context, String absPath) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("file://" + absPath), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } /** *根据apk包名卸载应用 */ public static void uninstallApk(Context context, String packageName) { String uriString = "package:" + packageName; Uri uninstallUrl = Uri.parse(uriString); Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUrl); context.startActivity(intent); } public static List<ResolveInfo> queryAppInfo(Context context, String packageName) { PackageManager pm = context.getPackageManager(); // 获得PackageManager对象 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); mainIntent.setPackage(packageName); // 通过查询,获得所有ResolveInfo对象. List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent, PackageManager.GET_INTENT_FILTERS); return resolveInfos; } /** *根据名名获取版本信息 */ public static int getInstalledAppVersionCode(Context context, String pkgName) { int versionCode = -1; PackageInfo pkgInfo = AppUtil.getPkgInfoByName(context, pkgName); if (pkgInfo != null) { versionCode = pkgInfo.versionCode; } return versionCode; } }