天天看點

代碼擷取Android版本等資訊

我手機的關于手機界面:

代碼擷取Android版本等資訊

說明:

其中手機型号、Android版本、軟體版本通過系統Build類得到,處理器資訊、核心版本通過讀取系統檔案得到,基帶版本資訊通過反射得到。

源碼:

package com.example.shen.phoneinfo;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;


public class MainActivity extends Activity {

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

        Log.e("phoneInfo", "MODEL:" + android.os.Build.MODEL);
        Log.e("phoneInfo", "CPUInfo:"+getCPUInfo());
        Log.e("phoneInfo", "VERSION_RELEASE:"+android.os.Build.VERSION.RELEASE);
        Log.e("phoneInfo", "VERSION_SDK:"+android.os.Build.VERSION.SDK_INT + "");
        Log.e("phoneInfo", "BaseBandVersion:"+getBaseBandVersion());
        Log.e("phoneInfo", "KernelVersion:"+getKernelVersion());
        Log.e("phoneInfo", "ID:" + android.os.Build.ID);
    }

    /**
     * 獲得處理器資訊
     * @return String
     */
    public String getCPUInfo() {
        try {
            FileReader fileReader = new FileReader("/proc/cpuinfo");
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String info;
            while ((info = bufferedReader.readLine()) != null) {
                String[] array = info.split(":");
                if(array[0].trim().equals("Hardware")) {
                    return array[1];
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *獲得基帶版本
     * @return String
     */
    public String getBaseBandVersion() {
        String version = "";
            try {
                Class clazz= Class.forName("android.os.SystemProperties");
                Object object = clazz.newInstance();
                Method method = clazz.getMethod("get", new Class[]{String.class, String.class});
                Object result = method.invoke(object, new Object[]{"gsm.version.baseband", "no message"});
                version = (String) result;
            } catch (Exception e) {
        }
        return version;
    }

    /**
     * 獲得核心版本
     * @return String
     */
    public String getKernelVersion() {
        Process process = null;
        String kernelVersion = "";
        try {
            process = Runtime.getRuntime().exec("cat /proc/version");
        } catch (IOException e) {
            e.printStackTrace();
        }
        InputStream inputStream = process.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader, 8 * 1024);
        String result = "";
        String info;
        try {
            while ((info = bufferedReader.readLine()) != null) {
            result += info;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (result != "") {
                String keyword = "version ";
                int index = result.indexOf(keyword);
                info = result.substring(index + keyword.length());
                index = info.indexOf(" ");
                kernelVersion = info.substring(0, index);
            }
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        return kernelVersion;
    }
}
           

運作結果:

代碼擷取Android版本等資訊

/proc/cpuinfo檔案截圖

代碼擷取Android版本等資訊

/proc/version檔案截圖

代碼擷取Android版本等資訊