当前位置:   article > 正文

Android 获取手机 设备的 信息_android 获取设备信息

android 获取设备信息

///

Android 获取手机的厂商、型号、Android系统版本号、IMEI、当前系统语言等工具类

在开发中,需要用到一些系统信息的工具类有以下6个功能:

1、获取手机制造厂商

2、获取手机型号

3、获取手机系统当前使用的语言

4、获取Android系统版本号

5、获取手机IMEI串号

6、获取手机中的语言列表

SystemUtil类

  1. /** 
  2.  * 系统工具类 
  3.  * Created by zhuwentao on 2016-07-18. 
  4.  */  
  5. public class SystemUtil {  
  6.   
  7.     /** 
  8.      * 获取当前手机系统语言。 
  9.      * 
  10.      * @return 返回当前系统语言。例如:当前设置的是“中文-中国”,则返回“zh-CN” 
  11.      */  
  12.     public static String getSystemLanguage() {  
  13.         return Locale.getDefault().getLanguage();  
  14.     }  
  15.   
  16.     /** 
  17.      * 获取当前系统上的语言列表(Locale列表) 
  18.      * 
  19.      * @return  语言列表 
  20.      */  
  21.     public static Locale[] getSystemLanguageList() {  
  22.         return Locale.getAvailableLocales();  
  23.     }  
  24.   
  25.     /** 
  26.      * 获取当前手机系统版本号 
  27.      * 
  28.      * @return  系统版本号 
  29.      */  
  30.     public static String getSystemVersion() {  
  31.         return android.os.Build.VERSION.RELEASE;  
  32.     }  
  33.   
  34.     /** 
  35.      * 获取手机型号 
  36.      * 
  37.      * @return  手机型号 
  38.      */  
  39.     public static String getSystemModel() {  
  40.         return android.os.Build.MODEL;  
  41.     }  
  42.   
  43.     /** 
  44.      * 获取手机厂商 
  45.      * 
  46.      * @return  手机厂商 
  47.      */  
  48.     public static String getDeviceBrand() {  
  49.         return android.os.Build.BRAND;  
  50.     }  
  51.   
  52.     /** 
  53.      * 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限) 
  54.      * 
  55.      * @return  手机IMEI 
  56.      */  
  57.     public static String getIMEI(Context ctx) {  
  58.         TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);  
  59.         if (tm != null) {  
  60.             return tm.getDeviceId();  
  61.         }  
  62.         return null;  
  63.     }  
  64. }  

其中获取手机IMEI需要在AndroidManifest.xml中加上权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />  

使用这些方法的时候直接调用就好了

  1. private void showSystemParameter() {  
  2.     String TAG = "系统参数:";  
  3.     Log.e(TAG, "手机厂商:" + SystemUtil.getDeviceBrand());  
  4.     Log.e(TAG, "手机型号:" + SystemUtil.getSystemModel());  
  5.     Log.e(TAG, "手机当前系统语言:" + SystemUtil.getSystemLanguage());  
  6.     Log.e(TAG, "Android系统版本号:" + SystemUtil.getSystemVersion());  
  7.     Log.e(TAG, "手机IMEI:" + SystemUtil.getIMEI(getApplicationContext()));  

返回结果:

//

Android系统获取GPU、屏幕信息

源码下载

https://github.com/sufadi/AndroidCpuTools

1.GPU 电压和频率

  1. C:\Users\fadi.su>adb shell cat /proc/gpufreq/gpufreq_opp_dump
  2. [0] freq = 450000, volt = 115000, idx = 0
  3. [1] freq = 400000, volt = 111875, idx = 1
  4. [2] freq = 350000, volt = 108750, idx = 2
  5. [3] freq = 290000, volt = 105000, idx = 3
  6. public static List getGpuFreqVolt() {
  7. List result = new ArrayList<>();
  8. try {
  9. String line;
  10. BufferedReader br = new BufferedReader(new FileReader("/proc/gpufreq/gpufreq_opp_dump"));
  11. while ((line = br.readLine()) != null) {
  12. result.add(line);
  13. }
  14. br.close();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. return result;
  19. }

2.GPU 供应商和版本信息

  1. final RendererUtil mRendererUtil = new RendererUtil();
  2. GLSurfaceView mGLSurfaceView = (GLSurfaceView) mView.findViewById(R.id.surfaceView);
  3. mGLSurfaceView.setEGLContextClientVersion(1);
  4. mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0);
  5. mGLSurfaceView.setRenderer(mRendererUtil);
  6. mGLSurfaceView.post(new Runnable() {
  7. @Override
  8. public void run() {
  9. result.add(getString(R.string.gl_vendor) + mRendererUtil.gl_vendor);
  10. result.add(getString(R.string.gl_renderer) + mRendererUtil.gl_renderer);
  11. result.add(getString(R.string.gl_version) + mRendererUtil.gl_version);
  12. result.add(getString(R.string.gl_extensions) + mRendererUtil.gl_extensions);
  13. mHandler.sendEmptyMessage(UPDATE_UI);
  14. }
  15. });
  16. package com.fadisu.cpurun.util;
  17. import android.opengl.GLSurfaceView;
  18. import android.util.Log;
  19. import javax.microedition.khronos.egl.EGLConfig;
  20. import javax.microedition.khronos.opengles.GL10;
  21. public class RendererUtil implements GLSurfaceView.Renderer {
  22. //GPU 渲染器
  23. public String gl_renderer;
  24. //GPU 供应商
  25. public String gl_vendor;
  26. //GPU 版本
  27. public String gl_version;
  28. //GPU 扩展名
  29. public String gl_extensions;
  30. @Override
  31. public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
  32. gl_renderer = gl.glGetString(GL10.GL_RENDERER);
  33. gl_vendor = gl.glGetString(GL10.GL_VENDOR);
  34. gl_version = gl.glGetString(GL10.GL_VERSION);
  35. gl_extensions = gl.glGetString(GL10.GL_EXTENSIONS);
  36. Log.d("suhuazhi", "onSurfaceCreated = " + gl_renderer);
  37. }
  38. @Override
  39. public void onSurfaceChanged(GL10 gl10, int i, int i1) {
  40. }
  41. @Override
  42. public void onDrawFrame(GL10 gl10) {
  43. }
  44. }

3.GPU 获取屏幕

  1. public class ScreenInfo {
  2. // 英寸
  3. public double size;
  4. public String sizeStr;
  5. //
  6. public int heightPixels;
  7. //
  8. public int widthPixels;
  9. public String screenRealMetrics;
  10. // 显示器的逻辑密度
  11. public float density;
  12. // 屏幕密度为点每英寸
  13. public int densityDpi;
  14. public String densityDpiStr;
  15. // 显示在显示器的字体的定标因子
  16. public float scaledDensity;
  17. // 每在 X 维屏幕英寸的确切物理像素
  18. public float xdpi;
  19. // 每在 Y 维屏幕英寸的确切物理像素
  20. public float ydpi;
  21. //在屏幕中显示的参考密度
  22. public int density_default;
  23. @Override
  24. public String toString() {
  25. return "ScreenInfo{" +
  26. "size=" + size +
  27. ", sizeStr='" + sizeStr + '\'' +
  28. ", heightPixels=" + heightPixels +
  29. ", screenRealMetrics='" + screenRealMetrics + '\'' +
  30. ", widthPixels=" + widthPixels +
  31. ", density=" + density +
  32. ", densityDpi=" + densityDpi +
  33. ", densityDpiStr='" + densityDpiStr + '\'' +
  34. ", scaledDensity=" + scaledDensity +
  35. ", xdpi=" + xdpi +
  36. ", ydpi=" + ydpi +
  37. ", density_default=" + density_default +
  38. '}';
  39. }
  40. }
  41. /**
  42. * 屏幕分辨率
  43. *
  44. * @param mContext
  45. * @return
  46. */
  47. public static ScreenInfo getScreenInfo(Context mContext) {
  48. ScreenInfo result = new ScreenInfo();
  49. int widthPixels;
  50. int heightPixels;
  51. WindowManager w = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
  52. Display d = w.getDefaultDisplay();
  53. DisplayMetrics metrics = new DisplayMetrics();
  54. d.getMetrics(metrics);
  55. // since SDK_INT = 1;
  56. widthPixels = metrics.widthPixels;
  57. heightPixels = metrics.heightPixels;
  58. // includes window decorations (statusbar bar/menu bar)
  59. if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
  60. try {
  61. widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
  62. heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
  63. } catch (Exception ignored) {
  64. ignored.printStackTrace();
  65. }
  66. }
  67. // includes window decorations (statusbar bar/menu bar)
  68. if (Build.VERSION.SDK_INT >= 17) {
  69. try {
  70. Point realSize = new Point();
  71. Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
  72. widthPixels = realSize.x;
  73. heightPixels = realSize.y;
  74. } catch (Exception ignored) {
  75. ignored.printStackTrace();
  76. }
  77. }
  78. result.widthPixels = widthPixels;
  79. result.heightPixels = heightPixels;
  80. result.screenRealMetrics = widthPixels + " X " + heightPixels;
  81. result.density = metrics.density;
  82. result.density_default = metrics.DENSITY_DEFAULT;
  83. result.densityDpi = metrics.densityDpi;
  84. result.densityDpiStr = metrics.densityDpi + " dpi";
  85. result.scaledDensity = metrics.scaledDensity;
  86. result.xdpi = metrics.xdpi;
  87. result.ydpi = metrics.ydpi;
  88. result.size = (Math.sqrt(Math.pow(widthPixels, 2) + Math.pow(heightPixels, 2)) / metrics.densityDpi);
  89. result.sizeStr = String.format("%.2f", result.size) + mContext.getResources().getString(R.string.sys_inches_unit);
  90. return result;
  91. }

4.运行结果

Android系统获取GPU、屏幕信息_第1张图片

android获取GPU信息;android获取GPU渲染器、供应商、版本和扩展名等信息

效果:

在这里插入图片描述

1、布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="15dp"
    tools:context=".FirstActivity">

    <TextView
        android:id="@+id/gpu_renderer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="renderer"
        android:textSize="20dp"/>

    <TextView
        android:id="@+id/gpu_vendor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="vendor"
        android:textSize="20dp"/>

    <TextView
        android:id="@+id/gpu_version"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="gpu_version"
        android:textSize="20dp"/>

    <TextView
        android:id="@+id/gpu_extensions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="extensions"
        android:textSize="20dp"/>

    <android.opengl.GLSurfaceView
        android:id="@+id/gl_surface_view"
        android:layout_width="1dp"
        android:layout_height="1dp"/>

</LinearLayout>


2、工具类

public class GpuInfoUtil implements GLSurfaceView.Renderer{
    public static String glRenderer;
    public static String glVendor;
    public static String glVersion;
    public static String glExtensions;

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        glRenderer = gl.glGetString(GL10.GL_RENDERER);      //GPU 渲染器
        glVendor = gl.glGetString(GL10.GL_VENDOR);          //GPU 供应商
        glVersion = gl.glGetString(GL10.GL_VERSION);        //GPU 版本
        glExtensions = gl.glGetString(GL10.GL_EXTENSIONS);  //GPU 扩展名
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {

    }

    @Override
    public void onDrawFrame(GL10 gl) {

    }
}
 

3、活动

public class FirstActivity extends AppCompatActivity {
    private TextView glRenderer;
    private TextView glVendor;
    private TextView glVersion;
    private TextView glExtensions;
    private GLSurfaceView glSurfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_activity);
        initView();
        setData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //延迟100毫秒再刷新一次,确保获取到GPU各项属性
        handler.postDelayed(runnable,100);
    }

    //更新界面数据
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run () {
            setData();
        }
    };

    private void setData(){
        glRenderer.setText("GPU 渲染器: "+GpuInfoUtil.glRenderer);
        glVendor.setText("GPU 供应商: "+GpuInfoUtil.glVendor);
        glVersion.setText("GPU 版本: "+GpuInfoUtil.glVersion);
        glExtensions.setText("GPU 扩展名: "+GpuInfoUtil.glExtensions);
    }

    private void initView(){
        glRenderer = (TextView)findViewById(R.id.gpu_renderer);
        glVendor = (TextView)findViewById(R.id.gpu_vendor);
        glVersion = (TextView)findViewById(R.id.gpu_version);
        glExtensions = (TextView)findViewById(R.id.gpu_extensions);

        glSurfaceView= findViewById(R.id.gl_surface_view);
        glSurfaceView.setEGLContextClientVersion(1);
        glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0);
        glSurfaceView.setRenderer(new GpuInfoUtil());
    }
}

 

///

详解Android获得系统GPU参数 gl.glGetString

详解Android获得系统GPU参数 gl.glGetString

通过文档的查找,以及源码的剖析,Android的GPU信息需要通过OpenGL来获取,android framework层提供GL10来获取相应的参数,而GL10要在使用自定义的View时才可以获得,下面是获得GPU信息的例子:

1.实现Render类

class DemoRenderer implements GLSurfaceView.Renderer { 
 
  public void onSurfaceCreated(GL10 gl, EGLConfig config) 
  { 
    Log.d("SystemInfo", "GL_RENDERER = " +gl.glGetString(GL10.GL_RENDERER));  
    Log.d("SystemInfo", "GL_VENDOR = " + gl.glGetString(GL10.GL_VENDOR));  
    Log.d("SystemInfo", "GL_VERSION = " + gl.glGetString(GL10.GL_VERSION));  
    Log.i("SystemInfo", "GL_EXTENSIONS = " + gl.glGetString(GL10.GL_EXTENSIONS));  
  } 
 
 
  @Override 
  public void onDrawFrame(GL10 arg0) { 
    // TODO Auto-generated method stub 
 
  } 
 
 
  @Override 
  public void onSurfaceChanged(GL10 arg0, int arg1, int arg2) { 
    // TODO Auto-generated method stub 
 
  } 
 


2.实现GLSurfaceView

class DemoGLSurfaceView extends GLSurfaceView { 
 
  DemoRenderer mRenderer; 
  public DemoGLSurfaceView(Context context) { 
    super(context); 
    setEGLConfigChooser(8, 8, 8, 8, 0, 0); 
    mRenderer = new DemoRenderer(); 
    setRenderer(mRenderer); 
  } 

3.在Activity中 new 一个 DemoGLSurfaceView 对象

4.设置这个View对象

public class Demo extends Activity {

  @Override

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    GLSurfaceView glView = new DemoGLSurfaceView(this);

    this.setContentView(glView);

  }

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/242159
推荐阅读
相关标签
  

闽ICP备14008679号