获取手机基本信息的工具类

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: blueZhang
 * DATE:2015/11/15
 * Time: 15:05
 * email:[email protected]
 */

/**
 *
 * <font face ="华文新魏" size = "2" color="yellow">用于获取手机信息的工具类<br/>主方法是获取手机屏幕的宽度和高度</font>
 * @see #checkNetState(Context) 检测网络的状态(只是检测能不能联网,不能检测什么网络3G/4G)
 * @see #dip2px(Context, float)
 * @see #px2dip(Context, float)
 * @see #getAllApps(Context) 获取手机所有的应用程序
 * @see #getMaxMemory() 获取手机最大内存
 * @see #getScreenHeight(Activity) 获取屏幕高度
 * @see #getScreenWidth(Activity) 获取屏幕宽度
 * @see #isMountedSDCard() 判断SD卡是否挂载
 * @see #zoomBitmap(Bitmap, int, int)
 *
 *
 */


public final class CommonMobileUtil {


    private CommonMobileUtil(){
    }

    /**
     * 判断SDCard是否挂载
     */
    public static boolean isMountedSDCard() {
        String status = Environment.getExternalStorageState();
        return status.equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 获取最大内存
     *
     * @return 返回一个手机的最大内存,单位:M
     */
    public static long getMaxMemory() {
        return Runtime.getRuntime().maxMemory() / 1024;
    }

    /**
     * 检查网络
     *
     * @param context
     * @return
     */
    public static boolean checkNetState(Context context) {
        boolean netstate = false;
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] allNetworkInfo = connectivity.getAllNetworkInfo();
            if (allNetworkInfo != null) {
                for (int i = 0; i < allNetworkInfo.length; i++) {
                    if (allNetworkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                        netstate = true;
                        break;
                    }
                }
            }
        }
        return netstate;
    }

    /**
     * 得到屏幕高度
     *
     * @param context
     * @return
     */
    public static int getScreenHeight(Activity context) {
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metric);
        return metric.heightPixels;
    }

    /**
     * 得到屏幕宽度
     *
     * @param context Activity
     * @return 返回屏幕的宽度
     */
    public static int getScreenWidth(Activity context) {
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metric);
        return metric.widthPixels;
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     * @param context 上下文对象
     * @param dpValue 需要进行转换的dp
     * @return 返回转换之后的px
     */

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     *  根据手机的分辨率从 px(像素) 的单位 转成为 dp
     * @param context 上下文对象
     * @param pxValue 需要进行转换的px大小
     * @return 返回转换之后的dp
     */
    public static int px2dip(Context context, float pxValue) {

        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 查询手机内非系统应用
     *
     * @param context 上下文对象
     * @return 系统应用packageInfo集合
     */
    public static List<PackageInfo> getAllApps(Context context) {

        List<PackageInfo> apps = new ArrayList<PackageInfo>();
        PackageManager pManager = context.getPackageManager();
        // 获取手机内所有应用
        List<PackageInfo> paklist = pManager.getInstalledPackages(0);
        for (int i = 0; i < paklist.size(); i++) {
            PackageInfo pak = (PackageInfo) paklist.get(i);
            // 判断是否为非系统预装的应用程序
            if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
                // customs applications
                apps.add(pak);
            }
        }
        return apps;
    }

    /**
     *
     * @param bitmap
     * @param width
     * @param height
     * @return
     */
    public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) width / w);
        float scaleHeight = ((float) height / h);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        return newbmp;
    }

    /**
     * 获取版本号和版本次数
     *
     * @param context
     * @return
     */
    public static String getVersionCode(Context context, int type) {

        try {

            PackageInfo pi = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            if (type == 1) {

                return String.valueOf(pi.versionCode);
            } else {

                return pi.versionName;
            }
        } catch (PackageManager.NameNotFoundException e) {

            e.printStackTrace();
            return null;
        }
    }


}

你可能感兴趣的:(android,工具类)