zoukankan      html  css  js  c++  java
  • Android-AppUtils工具类

    常用APP的工具类,包含版本号、版本名称、安装的应用程序ICON

    public class AppUtils {
    
        private AppUtils(){}
    
        /**
         * 设置状态栏的颜色
         * @param activity
         * @param colorResId
         */
        public static void setWindowStatusBarColor(Activity activity, int colorResId) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Window window = activity.getWindow();
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    window.setStatusBarColor(activity.getResources().getColor(colorResId));
    
                    //底部导航栏
                    //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 设置Dialog状态颜色
         * @param dialog
         * @param colorResId
         */
        public static void setWindowStatusBarColor(Dialog dialog, int colorResId) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Window window = dialog.getWindow();
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    window.setStatusBarColor(dialog.getContext().getResources().getColor(colorResId));
    
                    //底部导航栏
                    //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 获取包名
         * @param context
         * @return
         */
        public static String getPackageName(Context context){
            return context.getPackageName();
        }
    
        /**
         * 获取VersionName(版本名称)
         * @param context
         * @return
         * 失败时返回""
         */
        public static String getVersionName(Context context){
            PackageManager packageManager = getPackageManager(context);
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(context), 0);
                return packageInfo.versionName;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            return "";
        }
    
        /**
         * 获取VersionCode(版本号)
         * @param context
         * @return
         * 失败时返回-1
         */
        public static int getVersionCode(Context context){
            PackageManager packageManager = getPackageManager(context);
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(context), 0);
                return packageInfo.versionCode;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            return -1;
        }
    
        /**
         * 获取所有安装的应用程序,不包含系统应用
         * @param context
         * @return
         */
        public static List<PackageInfo> getInstalledPackages(Context context){
            PackageManager packageManager = getPackageManager(context);
            List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
            List<PackageInfo> packageInfoList  = new ArrayList<>();
            for(int i=0; i < packageInfos.size();i++){
                if ((packageInfos.get(i).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                    packageInfoList.add(packageInfos.get(i));
                }
            }
            return packageInfoList;
        }
    
        /**
         * 获取应用程序的icon图标
         * @param context
         * @return
         * 当包名错误时,返回null
         */
        public static Drawable getApplicationIcon(Context context){
            PackageManager packageManager = getPackageManager(context);
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(context), 0);
                return packageInfo.applicationInfo.loadIcon(packageManager);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 启动安装应用程序
         * @param activity
         * @param path    应用程序路径
         */
        public static void installApk(Activity activity, String path){
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(path)),
                    "application/vnd.android.package-archive");
            activity.startActivity(intent);
        }
    
        /**
         * 获取PackageManager对象
         * @param context
         * @return
         */
        private static PackageManager getPackageManager(Context context){
            return context.getPackageManager();
        }
    }
  • 相关阅读:
    Google是不是真的不能用了?非常奇怪的问题
    九度机试 题目1165:字符串匹配 2008年北京航空航天大学计算机研究生机试真题
    UNIX网络编程卷1 时间获取程序server TCP 协议相关性
    uva 1557
    C经典之14-双向链表存储1-10---ShinePans
    Java 内部类
    HiPAC高性能规则匹配算法之查找过程
    Objective-C之成魔之路【9-类构造方法和成员变量作用域、以及变量】
    NSRange,判断字符串的各种操作~
    NSRange类详解
  • 原文地址:https://www.cnblogs.com/android-deli/p/10322157.html
Copyright © 2011-2022 走看看