中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android開發(fā)工具類

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
public final class SystemTool {
   
    /**
     * 指定格式返回當前系統(tǒng)時間
     */
    public static String getDataTime(String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date());
    }

    /**
     * 返回當前系統(tǒng)時間(格式以HH:mm形式)
     */
    public static String getDataTime() {
        return getDataTime("HH:mm");
    }

    /**
     * 獲取手機IMEI碼
     */
    public static String getPhoneIMEI(Context cxt) {
        TelephonyManager tm = (TelephonyManager) cxt.getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }

    /**
     * 獲取手機系統(tǒng)SDK版本
     *
     * @return 如API 17 則返回 17
     */
    public static int getSDKVersion() {
        return android.os.Build.VERSION.SDK_INT;
    }

    /**
     * 獲取系統(tǒng)版本
     *
     * @return 形如2.3.3
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    /**
     * 調用系統(tǒng)發(fā)送短信
     */
    public static void sendSMS(Context cxt, String smsBody) {
        Uri smsToUri = Uri.parse("smsto:");
        Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
        intent.putExtra("sms_body", smsBody);
        cxt.startActivity(intent);
    }

    /**
     * 判斷網(wǎng)絡是否連接
     */
    public static boolean checkNet(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        return info != null;// 網(wǎng)絡是否連接
    }

    /**
     * 判斷是否為wifi聯(lián)網(wǎng)
     */
    public static boolean isWiFi(Context cxt) {
        ConnectivityManager cm = (ConnectivityManager) cxt.getSystemService(Context.CONNECTIVITY_SERVICE);
        // wifi的狀態(tài):ConnectivityManager.TYPE_WIFI
        // 3G的狀態(tài):ConnectivityManager.TYPE_MOBILE
        State state = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        return State.CONNECTED == state;
    }

    /**
     * 隱藏系統(tǒng)鍵盤 <br>
     * <b>警告</b> 必須是確定鍵盤顯示時才能調用
     */
    public static void hideKeyBoard(Activity aty) {
        ((InputMethodManager) aty.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(aty.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 判斷當前應用程序是否后臺運行
     */
    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                    // 后臺運行
                    return true;
                } else {
                    // 前臺運行
                    return false;
                }
            }
        }
        return false;
    }

    /**
     * 判斷手機是否處理睡眠
     */
    public static boolean isSleeping(Context context) {
        KeyguardManager kgMgr = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        boolean isSleeping = kgMgr.inKeyguardRestrictedInputMode();
        return isSleeping;
    }

    /**
     * 安裝apk
     *
     * @param context
     * @param file
     */
    public static void installApk(Context context, File file) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setType("application/vnd.android.package-archive");
        intent.setData(Uri.fromFile(file));
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 獲取當前應用程序的版本號
     */
    public static String getAppVersion(Context context) {
        String version = "0";
        try {
            version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            throw new KJException(SystemTool.class.getName() + "the application not found");
        }
        return version;
    }

    /**
     * 回到home,后臺運行
     */
    public static void goHome(Context context) {
        Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);
        mHomeIntent.addCategory(Intent.CATEGORY_HOME);
        mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        context.startActivity(mHomeIntent);
    }

    /**
     * 獲取應用簽名
     *
     * @param context
     * @param pkgName
     */
    public static String getSign(Context context, String pkgName) {
        try {
            PackageInfo pis = context.getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
            return hexdigest(pis.signatures[0].toByteArray());
        } catch (NameNotFoundException e) {
            throw new KJException(SystemTool.class.getName() + "the " + pkgName + "'s application not found");
        }
    }

    /**
     * 將簽名字符串轉換成需要的32位簽名
     */
    private static String hexdigest(byte[] paramArrayOfByte) {
        final char[] hexDigits = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 };
        try {
            MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
            localMessageDigest.update(paramArrayOfByte);
            byte[] arrayOfByte = localMessageDigest.digest();
            char[] arrayOfChar = new char[32];
            for (int i = 0, j = 0;; i++, j++) {
                if (i >= 16) { return new String(arrayOfChar); }
                int k = arrayOfByte[i];
                arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
                arrayOfChar[++j] = hexDigits[(k & 0xF)];
            }
        } catch (Exception e) {}
        return "";
    }

    /**
     * 獲取設備的可用內存大小
     *
     * @param cxt
     *            應用上下文對象context
     * @return 當前內存大小
     */
    public static int getDeviceUsableMemory(Context cxt) {
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);
        // 返回當前系統(tǒng)的可用內存
        return (int) (mi.availMem / (1024 * 1024));
    }

    /**
     * 清理后臺進程與服務
     *
     * @param cxt
     *            應用上下文對象context
     * @return 被清理的數(shù)量
     */
    public static int gc(Context cxt) {
        long i = getDeviceUsableMemory(cxt);
        int count = 0; // 清理掉的進程數(shù)
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        // 獲取正在運行的service列表
        List<RunningServiceInfo> serviceList = am.getRunningServices(100);
        if (serviceList != null) for (RunningServiceInfo service : serviceList) {
            if (service.pid == android.os.Process.myPid()) continue;
            try {
                android.os.Process.killProcess(service.pid);
                count++;
            } catch (Exception e) {
                e.getStackTrace();
                continue;
            }
        }

        // 獲取正在運行的進程列表
        List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
        if (processList != null) for (RunningAppProcessInfo process : processList) {
            // 一般數(shù)值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的進程都長時間沒用或者空進程了
            // 一般數(shù)值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的進程都是非可見進程,也就是在后臺運行著
            if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                // pkgList 得到該進程下運行的包名
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) { // 防止意外發(fā)生
                        e.getStackTrace();
                        continue;
                    }
                }
            }
        }
        //"清理了" + (getDeviceUsableMemory(cxt) - i) + "M內存";
        return count;
    }
}

標簽: ssl 網(wǎng)絡

版權申明:本站文章部分自網(wǎng)絡,如有侵權,請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Android利用Volley框架加載網(wǎng)絡圖片

下一篇: ImageView加載網(wǎng)絡圖片