EnvironmentUtil.java 11.6 KB
Newer Older
门心叼龙's avatar
门心叼龙 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
package com.yesway.android.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

/**
 * Description: <单位转换工具类><br>
 * <ul>
 * <h1>基本功能:</h1>
 * <li>1.获取包名</li>
 * <li>2.获取当前应用的版本号</li>
 * <li>3.获取当前应用的版本名称</li>
 * <li>4.获取应用的名称</li>
 * <li>5.获取assets下资源</li>
 * <li>6.获取系统语言</li>
 * <li>7.获取assets下资源</li>
 * <li>8.app的是否在运行</li>
 * <li>9.app是否在前台运行</li>
 * <li>10.当前Activity是否在栈顶></li>
 * <li>11.启动应用</li>
 * <li>12.获取assets下资源</li>
 * <h1>存贮相关</h1>
 * <li>1.外部存储是否可读写</li>
 * <li>2.获取外部存储文件夹</li>
 * <li>3.获取外部存储路径</li>
 * <h1>APK相关操作</h1>
 * <li>1.安装APK</li>
 * <li>2.卸载APK</li>
 * <li>3.获得APK包名</li>
 * <li>4.判断是否已经安装</li>
 * </ul>
M
mxdl 已提交
53
 * Author: mxdl<br>
门心叼龙's avatar
门心叼龙 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
 * Date: 2018/6/11<br>
 * Version: V1.0.0<br>
 * Update: <br>
 */

public class EnvironmentUtil{
    private static final String TAG = "EnvironmentUtil";

    /**
     * 获取包名
     *
     * @return 包名
     */
    public static String getPackageName(Context context) {
        return context.getPackageName();
    }

    /**
     * 获取当前应用的版本号
     */
    public static int getAppVersionCode(Context context) {
        // 获取手机的包管理者
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo packInfo = pm.getPackageInfo(getPackageName(context), 0);
            return packInfo.versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取当前应用的版本名称
     */
    public static String getAppVersionName(Context context) {
        // 获取手机的包管理者
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo packInfo = pm.getPackageInfo(getPackageName(context), 0);
            return packInfo.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            // 不可能发生.
            return "";
        }
    }

    /**
     * 获取应用的名称
     *
     * @return
     */
    public static String getApplicationName(Context context) {
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo = null;
        try {
            packageManager = context.getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(getPackageName(context), 0);
        } catch (NameNotFoundException e) {
            applicationInfo = null;
        }
        String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
        System.out.println(applicationName);
        return applicationName;
    }

    /**
     * 获取assets下资源
     *
     * @param context
     * @param fileName
     * @return
     */
    public static String getAssetsString(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
            bf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();
    }

    /**
     * 获取系统语言
     *
     * @param context
     * @return
     * @param context
     * @return
     */
    public static String getSystemLanguage(Context context) {
        Locale locale = context.getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        return language;
    }

    /**
     * 当前app的是否在运行
     *
     * @return false: 说明app不在当前系统的栈中; true:当前app正处于用户使用状态(包含在Home在后台)
     */

    public boolean isAppRunning(Context context) {
        ActivityManager activityManager = (ActivityManager) (context.getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(100);
        if (runningTaskInfos == null) {
            return false;
        }
        for (ActivityManager.RunningTaskInfo taskInfo : runningTaskInfos)
            if (context.getPackageName().equals(taskInfo.baseActivity.getPackageName())) {
                return true;
            }
        return false;
    }

    /**
     * app是否在前台
     *
     * @param context
     * @return
     */
    public static boolean isForeground(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_FOREGROUND) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }

    /**
     * 判断当前Activity是否排在栈顶
     *
     * @return
     */
    protected boolean isTopActivity(Context context, Class clazz) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
        if (runningTaskInfos != null && !runningTaskInfos.isEmpty()
                && clazz.getName().equals(runningTaskInfos.get(0).topActivity.getClassName())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 返回app运行状态 1:程序在前台运行 2:程序在后台运行 3:程序未启动 注意:需要配置权限<uses-permission
     * android:name="android.permission.GET_TASKS" />
     */
    public static int getAppStatus(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(20);
        // 判断程序是否在栈顶
        if (list.get(0).topActivity.getPackageName().equals(context.getPackageName())) {
            return 1;
        } else {
            // 判断程序是否在栈里
            for (ActivityManager.RunningTaskInfo info : list) {
                if (info.topActivity.getPackageName().equals(context.getPackageName())) {
                    return 2;
                }
            }
            return 3;// 栈里找不到,返回3
        }
    }

    /**
     * 启动应用
     *
     * @param context
     * @param className
     */
    public static void openApp(Context context, String className) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        ComponentName cn = new ComponentName(context.getPackageName(), className);
        intent.setComponent(cn);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

    /**
     * 存储信息
     */
    public static class Storage {

        /**
         * 外部存储是否可读写
         *
         * @return 可读写返回true, 否则返回false
         */
        public static boolean isExternalStorageWritable() {
            String state = Environment.getExternalStorageState();
            return Environment.MEDIA_MOUNTED.equals(state);
        }

        /**
         * 获取外部目录缓存路径
         *
         * @param context
         *            context
         * @return 外部存储换成路径
         */
        public static File getExternalCacheDir(Context context) {
            File file = null;
            if (SdkVersionUtil.hasFroyo()) {
                file = context.getExternalCacheDir();
            }

            if (file == null) {
                final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
                file = new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);

                file.mkdirs();

                if (file.isDirectory()) {
                    return file;
                }
            }
            return null;
        }

        /**
         * 获取缓存路径
         *
         * @param context
         *            context
         * @return 存储路径
         */
        public static String getCachePath(Context context) {
            File file = null;
            if (isExternalStorageWritable()) {
                file = getExternalCacheDir(context);
            }

            return (file != null) ? file.getAbsolutePath() : context.getCacheDir().getAbsolutePath();
        }

    }

    /**
     * APK相关操作
     */
    public static class Package {

        /**
         * 安装APK
         *
         * @param context
         * @param file
         */
        public static void installApp(Context context, File file) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            context.startActivity(intent);
        }

        /**
         * 卸载APK
         *
         * @param context
         * @param packageName
         */
        public static void unInstallApp(Context context, String packageName) {
            Uri packageUri = Uri.parse("package:" + packageName);
            Intent intent = new Intent(Intent.ACTION_DELETE, packageUri);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }

        /**
         * 获得APK包名
         *
         * @param context
         * @param apkFile
         * @return
         */
        public static String getApkFilePackage(Context context, File apkFile) {
            PackageManager pm = context.getPackageManager();
            PackageInfo info = pm.getPackageArchiveInfo(apkFile.getPath(), PackageManager.GET_ACTIVITIES);
            if (info != null) {
                return info.applicationInfo.packageName;
            }
            return null;
        }

        /**
         * 判断是否已经安装
         *
         * @param context
         * @param packageName
         * @return
         */
        public static boolean isAppInstalled(Context context, String packageName) {
            if (TextUtils.isEmpty(packageName))
                return false;
            try {
                context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
                return true;
            } catch (NameNotFoundException e) {
                return false;
            }
        }
    }
}