Android开发中apk安装包常用工具

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
开发项目中用到的有关获取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;
    }
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇: ImageView加载网络图片

下一篇:Android网络加载图片并滚动显示