Android-AppUtils工具类

Posted 刘德利_Android

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
    

以上是关于Android-AppUtils工具类的主要内容,如果未能解决你的问题,请参考以下文章

Flutter Utils 全网最齐全的工具类

Java工具类怎么记

字符串工具类数组工具类集合工具类转型操作工具类编码与解码操作工具类

FlutterUtils 工具库

java 啥叫工具类

BeanUtil 好用的工具类