代碼擷取Android版本等資訊

來源:互聯網
上載者:User

標籤:代碼擷取   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;    }}


運行結果:


/proc/cpuinfo檔案



/proc/version檔案





著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

代碼擷取Android版本等資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.