MobileCheck手机参数检测

来源:互联网 发布:ios开发者必备软件 编辑:程序博客网 时间:2024/04/28 05:45
package com.simon.mobile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.os.SystemClock;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
    @SuppressWarnings("unused")
    private Context mContext;
    private StringBuilder sb;
    private int width, height;
    private TextView tv_mobile_parameter, tv_mobile_battery;
    @SuppressWarnings("unused")
    private String phoneResolution, phoneDPI, phoneModel, phoneSDK,
            availMemory, totalMemory, phoneVersion, sdCardBasePath, cpuName,
            maxCpuFreq, minCpuFreq, curCpuFreq, powerOn, phoneEMEI,
            screenSizeOfDevice;
    @SuppressWarnings("unused")
    private boolean sdCardMounted;
    private long sdCardAvailableSize;
    private long sdCardTotalSize;
    @SuppressWarnings("unused")
    private int batteryLevel, cpuNum;
    private ImageView iv_taiji;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = getApplicationContext();
        initView();

    }

    private void initView() {
        tv_mobile_parameter = (TextView) findViewById(R.id.tv_mobile_parameter);
        tv_mobile_battery = (TextView) findViewById(R.id.tv_mobile_battery);
        iv_taiji = (ImageView) findViewById(R.id.iv_taiji);
        Animation rotateAnim = AnimationUtils.loadAnimation(mContext,
                R.anim.rotate);
        iv_taiji.startAnimation(rotateAnim);
        iv_taiji.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                iv_taiji.clearAnimation();
                Animation AnimSet = AnimationUtils.loadAnimation(mContext,
                        R.anim.animation_set);
                iv_taiji.startAnimation(AnimSet);
                initData();
            }
        });
    }

    private void initData() {
        phoneModel = getPhoneModel();
        phoneEMEI = getPhoneEMEI();
        phoneSDK = getPhoneSDK();
        phoneVersion = getPhoneVersion();
        cpuName = getCpuName();
        cpuNum = getCpuNum();
        maxCpuFreq = getMaxCpuFreq();
        minCpuFreq = getMinCpuFreq();
        curCpuFreq = getCurCpuFreq();
        screenSizeOfDevice = getScreenSizeOfDevice();
        phoneResolution = getPhoneResolution();
        phoneDPI = getPhoneDPI();
        availMemory = getAvailMemory();
        totalMemory = getTotalMemory();
        sdCardMounted = isSDCardMounted();
        sdCardBasePath = getSDCardBasePath();
        sdCardAvailableSize = getSDCardAvailableSize();
        sdCardTotalSize = getSDCardTotalSize();
        getBatteryLevel();

        powerOn = getPowerOn();

        sb = new StringBuilder();
        sb.append("\n" + "手机型号:" + phoneModel);
        sb.append("\n" + "手机IMEI号:" + phoneEMEI);
        sb.append("\n" + "手机开发api版本:" + phoneSDK);
        sb.append("\n" + "手机Android版本号:" + phoneVersion);
        sb.append("\n" + "手机CPU版本号:" + cpuName);
        sb.append("\n" + "手机CPU核心数量:" + cpuNum);
        sb.append("\n" + "手机CPU最高频率:" + Integer.parseInt(maxCpuFreq) / 1024
                / 1024 + "." + Integer.parseInt(maxCpuFreq) % 1024 % 1024
                + "GHZ");
        sb.append("\n" + "手机CPU最低频率:" + Integer.parseInt(minCpuFreq) / 1024
                / 1024 + "." + Integer.parseInt(minCpuFreq) % 1024 % 1024
                + "GHZ");
        sb.append("\n" + "手机CPU当前频率:" + Integer.parseInt(curCpuFreq) / 1024
                / 1024 + "." + Integer.parseInt(curCpuFreq) % 1024 % 1024
                + "GHZ");
        sb.append("\n" + "手机屏幕物理尺寸:" + screenSizeOfDevice + "英寸");
        sb.append("\n" + "手机屏幕分辨率为:" + phoneResolution);
        sb.append("\n" + "手机DPI类型:" + phoneDPI);
        sb.append("\n" + "手机当前可用运行内存大小:" + availMemory);
        sb.append("\n" + "手机总运行内存大小:" + totalMemory);
        sb.append("\n" + "手机可用内部存储空间:" + sdCardAvailableSize / 1024 + "."
                + sdCardAvailableSize % 1024 + "GB");
        sb.append("\n" + "手机内部存储总容量:" + sdCardTotalSize / 1024 + "."
                + sdCardTotalSize % 1024 + "GB");
        // sb.append("\n" + "手机电池电量:" + batteryLevel + "%");
        sb.append("\n" + "手机开机时间:" + powerOn);

        tv_mobile_parameter.setText(sb);
    }

    /**
     * 获取android手机型号
     *
     * @return PhoneModel
     */
    public String getPhoneModel() {
        String PhoneModel = android.os.Build.MODEL;
        return PhoneModel;
    }

    /**
     * 获取android手机EMEI型号
     *
     * @return PhoneModel
     */
    public String getPhoneEMEI() {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String IMEIs = tm.getDeviceId();
        return IMEIs;
    }

    /**
     * 获取android手机开发api版本
     *
     * @return PhoneModel
     */
    public String getPhoneSDK() {
        @SuppressWarnings("deprecation")
        String PhoneSDK = android.os.Build.VERSION.SDK;
        return PhoneSDK;
    }

    /**
     * 获取android手机开发版本号
     *
     * @return PhoneVersion
     */
    public String getPhoneVersion() {
        String PhoneVersion = android.os.Build.VERSION.RELEASE;
        return PhoneVersion;
    }

    /**
     * 获取CPU名字
     *
     * @return
     */
    public String getCpuName() {
        try {
            FileReader fr = new FileReader("/proc/cpuinfo");
            @SuppressWarnings("resource")
            BufferedReader br = new BufferedReader(fr);
            String text = br.readLine();
            String[] array = text.split(":\\s+", 2);
            for (int i = 0; i < array.length; i++) {
            }
            return array[1];
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取CPU个数
     *
     * @return
     */
    private int getCpuNum() {
        // Private Class to display only CPU devices in the directory listing
        class CpuFilter implements FileFilter {
            @Override
            public boolean accept(File pathname) {
                // Check if filename is "cpu", followed by a single digit number
                if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                    return true;
                }
                return false;
            }
        }

        try {
            // Get directory containing CPU info
            File dir = new File("/sys/devices/system/cpu/");
            // Filter to only list the devices we care about
            File[] files = dir.listFiles(new CpuFilter());
            // Return the number of cores (virtual CPU devices)
            return files.length;
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 获取最大CPU频率(单位KHZ)
     *
     * @return
     */
    public String getMaxCpuFreq() {
        String result = "";
        ProcessBuilder cmd;
        try {
            String[] args = { "/system/bin/cat",
                    "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
            cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            InputStream in = process.getInputStream();
            byte[] re = new byte[24];
            while (in.read(re) != -1) {
                result = result + new String(re);
            }
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            result = "N/A";
        }
        return result.trim();
    }

    /**
     * 获取CPU最小频率(单位KHZ)
     *
     * @return
     */
    public String getMinCpuFreq() {
        String result = "";
        ProcessBuilder cmd;
        try {
            String[] args = { "/system/bin/cat",
                    "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };
            cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            InputStream in = process.getInputStream();
            byte[] re = new byte[24];
            while (in.read(re) != -1) {
                result = result + new String(re);
            }
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            result = "N/A";
        }
        return result.trim();
    }

    /**
     * 实时获取CPU当前频率(单位KHZ)
     *
     * @return
     */
    public String getCurCpuFreq() {
        String result = "N/A";
        try {
            FileReader fr = new FileReader(
                    "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
            @SuppressWarnings("resource")
            BufferedReader br = new BufferedReader(fr);
            String text = br.readLine();
            result = text.trim();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取android手机屏幕的物理尺寸
     *
     * @return
     */
    private String getScreenSizeOfDevice() {
        Point point = new Point();
        getWindowManager().getDefaultDisplay().getRealSize(point);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        double x = Math.pow(point.x / dm.xdpi, 2);
        double y = Math.pow(point.y / dm.ydpi, 2);
        double screenInches = Math.sqrt(x + y);
        String screenInche = String.format("%.1f", screenInches);
        return screenInche;
    }

    /**
     * 获取android手机屏幕分辨率
     *
     * @return PhoneResolution
     */
    public String getPhoneResolution() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;
        height = dm.heightPixels;
        String PhoneResolution = width + "*" + height;
        return PhoneResolution;
    }

    /**
     * 获取android手机DPI类型
     *
     * @return PhoneDPI
     */
    public String getPhoneDPI() {
        float sizeInt = Float.parseFloat(screenSizeOfDevice);
        double dpi = Math.sqrt(height * height + width * width) / sizeInt;
        // %.2f %.表示小数点前任意位数;2表示两位小数;格式后的结果为f 表示浮点型
        String dpiF = String.format("%.2f", dpi);
        String dpiType = "";
        if (dpi > 320) {
            dpiType = "属于xhdpi(优)";
        } else if (dpi > 240) {
            dpiType = "属于hdpi(良)";
        } else if (dpi > 160) {
            dpiType = "属于mdpi(中)";
        } else if (dpi > 120) {
            dpiType = "属于ldpi(差)";
        }
        String PhoneDPI = "手机DPI是:" + dpiF + "," + dpiType;
        return PhoneDPI;
    }

    /**
     * 获取android当前可用运行内存大小
     *
     * @return AvailMemory
     */
    public String getAvailMemory() {

        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);
        // 当前系统的可用内存
        long availMem = mi.availMem;
        // 将获取的内存大小规格化
        String AvailMemory = Formatter.formatFileSize(getBaseContext(),
                availMem);
        return AvailMemory;
    }

    /**
     * 获取android总运行内存大小
     *
     * @return TotalMemory
     */
    public String getTotalMemory() {
        // 系统内存信息文件
        String str1 = "/proc/meminfo";
        String str2;
        String[] arrayOfString;
        long initial_memory = 0;

        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(
                    localFileReader, 8192);
            // 读取meminfo第一行,系统总内存大小
            str2 = localBufferedReader.readLine();

            arrayOfString = str2.split("\\s+");
            for (String num : arrayOfString) {
                Log.i(str2, num + "\t");
            }

            initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte
            localBufferedReader.close();

        } catch (IOException e) {
        }
        // Byte转换为KB或者MB,内存大小规格化
        String TotalMemory = Formatter.formatFileSize(getBaseContext(),
                initial_memory);
        return TotalMemory;
    }

    /**
     * 判断SDCard是否挂载
     *
     * @return
     */
    public static boolean isSDCardMounted() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }

    /**
     * 获取SDCard的根目录路径
     *
     * @return
     */
    public static String getSDCardBasePath() {
        if (isSDCardMounted()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        } else {
            return null;
        }
    }

    /**
     * 获取SDCard的完整空间大小
     *
     * @return
     */
    public static long getSDCardTotalSize() {
        long size = 0;
        if (isSDCardMounted()) {
            StatFs statFs = new StatFs(getSDCardBasePath());
            if (Build.VERSION.SDK_INT >= 18) {
                size = statFs.getTotalBytes();
            } else {
                size = statFs.getBlockCount() * statFs.getBlockSize();
            }
            return size / 1024 / 1024;
        } else {
            return 0;
        }
    }

    /**
     * 获取SDCard的可用空间大小
     *
     * @return
     */
    public static long getSDCardAvailableSize() {
        long size = 0;
        if (isSDCardMounted()) {
            StatFs statFs = new StatFs(getSDCardBasePath());
            if (Build.VERSION.SDK_INT >= 18) {
                size = statFs.getAvailableBytes();
            } else {
                size = statFs.getAvailableBlocks() * statFs.getBlockSize();
            }
            return size / 1024 / 1024;
        } else {
            return 0;
        }
    }

    /**
     * 获取电池电量
     *
     * @return
     */
    public void getBatteryLevel() {
        BroadcastReceiver batteryReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                // level加%就是当前电量了
                int level = intent.getIntExtra("level", 0);
                // 获取电压值
                int voltage = intent.getIntExtra("voltage", -1);
                // 获取电池的温度
                int temperature = intent.getIntExtra("temperature", -1);

                Log.i("Simon", "battary" + level + "%");
                tv_mobile_battery.setText("手机电池" + "电量:" + level + "%"
                        + "  电压:" + voltage / 1000 + "." + voltage / 1000 + "v"
                        + "  温度:" + temperature / 10 + "." + temperature % 10
                        + "℃");

            }
        };
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryReceiver, filter);
    }

    /**
     * 获取开机时间
     *
     * @return
     */
    private String getPowerOn() {
        long ut = SystemClock.elapsedRealtime() / 1000;
        if (ut == 0) {
            ut = 1;
        }
        int m = (int) ((ut / 60) % 60);
        int h = (int) ((ut / 3600));
        return h + "小时" + m + "分";
    }

    /**
     * 获取手机精准数据
     */
    private void getData() {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        StringBuilder sb = new StringBuilder();

        sb.append("\nDeviceId(IMEI) = " + tm.getDeviceId());
        sb.append("\nDeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion());
        sb.append("\nLine1Number = " + tm.getLine1Number());
        sb.append("\nNetworkCountryIso = " + tm.getNetworkCountryIso());
        sb.append("\nNetworkOperator = " + tm.getNetworkOperator());
        sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());
        sb.append("\nNetworkType = " + tm.getNetworkType());
        sb.append("\nPhoneType = " + tm.getPhoneType());
        sb.append("\nSimCountryIso = " + tm.getSimCountryIso());
        sb.append("\nSimOperator = " + tm.getSimOperator());
        sb.append("\nSimOperatorName = " + tm.getSimOperatorName());
        sb.append("\nSimSerialNumber = " + tm.getSimSerialNumber());
        sb.append("\nSimState = " + tm.getSimState());
        sb.append("\nSubscriberId(IMSI) = " + tm.getSubscriberId());
        sb.append("\nVoiceMailNumber = " + tm.getVoiceMailNumber());
        // Log.i("Simon", sb.toString());
    }

}




DEMO:点击打开链接



0 0