码迷,mamicode.com
首页 > 移动开发 > 详细

代码获取Android版本等信息

时间:2015-08-09 22:42:50      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:代码获取   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版本等信息

标签:代码获取   android版本   基带版本   内核版本   软件版本   

原文地址:http://blog.csdn.net/shenyuanqing/article/details/47378713

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!