美文网首页
Android App开发常用的工具类

Android App开发常用的工具类

作者: 唔笛plk | 来源:发表于2018-12-04 14:16 被阅读0次

    一、序言:

    对于开发而言,最好把使用频繁使用的方法定义一个类,这样不仅能方便实用而且提示代码的可阅读性,在网上也有很多的工具类,这个 也就看个人开发所需,那我这里就添加一些我自己项目常用到的一些开发工具(本文章长期更新),欢迎大家点赞收藏。

    二、判断App是否在前台

        /**
         * 判断当前应用程序处于前台还是后台
         *
         * @param context
         * @return
         */
        public static boolean isApplicationBroughtToBackground(final Context context) {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
            if (!tasks.isEmpty()) {
                ComponentName topActivity = tasks.get(0).topActivity;
                return !topActivity.getPackageName().equals(context.getPackageName());
            }
            return false;
        }
    

    三、判断手机是否有导航栏

     /**
         * 获取是否存在NavigationBar
         *
         * @param context
         * @return
         */
        public static boolean checkDeviceHasNavigationBar(Context context) {
            boolean hasNavigationBar = false;
            Resources rs = context.getResources();
            int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
            if (id > 0) {
                hasNavigationBar = rs.getBoolean(id);
            }
            try {
                Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
                Method m = systemPropertiesClass.getMethod("get", String.class);
                String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
                if ("1".equals(navBarOverride)) {
                    hasNavigationBar = false;
                } else if ("0".equals(navBarOverride)) {
                    hasNavigationBar = true;
                }
            } catch (Exception e) {
            }
            return hasNavigationBar && getVirtualBarHeight(context) > 0;
        }
    
    

    四、获取虚拟导航栏高度

    /**
         * 获取虚拟功能键高度
         *
         * @param context
         * @return
         */
        public static int getVirtualBarHeight(Context context) {
            int vh = 0;
            // 获取不可视视图高度,即非App显示面积高度
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                Display display = windowManager.getDefaultDisplay();
                DisplayMetrics dm = new DisplayMetrics();
                try {
                    @SuppressWarnings("rawtypes")
                    Class c = Class.forName("android.view.Display");
                    @SuppressWarnings("unchecked")
                    Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
                    method.invoke(display, dm);
                    vh = dm.heightPixels - windowManager.getDefaultDisplay().getHeight();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
            }
            // 判断是否有虚拟按键
            boolean hasMenuKey = ViewConfiguration.get(context)
                    .hasPermanentMenuKey();
            boolean hasBackKey = KeyCharacterMap
                    .deviceHasKey(KeyEvent.KEYCODE_BACK);
            if (hasMenuKey || hasBackKey) {
                // 80为状态栏高度
                if (vh > 80) {
                    Resources res = context.getResources();
                    int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
                    if (resourceId > 0) {
                        vh = res.getDimensionPixelSize(resourceId);
                    }
                } else {
                    vh = 0;
                }
    
            }
    
            return vh;
        }
    

    五、判断网络是否连接

    
        /**
         * 检查网络
         *
         * @return
         */
        public boolean isNetworkConnect(Context context) {
            boolean isWifi = false;
            boolean isMobile = false;
            ConnectivityManager connectivityManager;
            connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                // 获取Wifi网络信息
                NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                // 获取移动网络信息
                NetworkInfo mobileNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                isWifi = wifiNetworkInfo.isConnected();
                isMobile = mobileNetworkInfo.isConnected();
    
            } else {
                // 获取所有的网络连接信息
                Network[] networks = connectivityManager.getAllNetworks();
                for (int j = 0; j < networks.length; j++) {
                    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networks[j]);
                    isWifi = networkInfo.getTypeName().equals("WIFI") && networkInfo.isConnected();
                    isMobile = networkInfo.getTypeName().equals("MOBILE") && networkInfo.isConnected();
                }
            }
            return isWifi || isMobile;
        }
    

    六、获取App版本号和版本名称

    /**
         * 获取APP-VersionCode
         *
         * @param activity
         * @return
         */
        public static int GetAppVersionCode(Activity activity) {
            String pName = activity.getPackageName();
            int versionCode = 0;
    
            try {
                PackageInfo pinfo = activity.getPackageManager().getPackageInfo(
                        pName, PackageManager.GET_CONFIGURATIONS);
                versionCode = pinfo.versionCode;
            } catch (NameNotFoundException e) {
            }
            return versionCode;
        }
    
        /**
         * 获取APP-VersionName
         *
         * @param activity
         * @return
         */
        public static String GetAppVersionName(Activity activity) {
            String pName = activity.getPackageName();
            String versionName = "";
    
            try {
                PackageInfo pinfo = activity.getPackageManager().getPackageInfo(
                        pName, PackageManager.GET_CONFIGURATIONS);
                versionName = pinfo.versionName;
            } catch (NameNotFoundException e) {
            }
            return versionName;
        }
    

    相关文章

      网友评论

          本文标题:Android App开发常用的工具类

          本文链接:https://www.haomeiwen.com/subject/rdwecqtx.html