Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 代碼獲取Android版本等信息

代碼獲取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;
    }
}

 

 

運行結果:
\

 

/proc/cpuinfo文件截圖

\

 

/proc/version文件截圖

\

 

 

 

 

  1. 上一頁:
  2. 下一頁: