赞
踩
///
Android 获取手机的厂商、型号、Android系统版本号、IMEI、当前系统语言等工具类
在开发中,需要用到一些系统信息的工具类有以下6个功能:
1、获取手机制造厂商
2、获取手机型号
3、获取手机系统当前使用的语言
4、获取Android系统版本号
5、获取手机IMEI串号
6、获取手机中的语言列表
SystemUtil类
其中获取手机IMEI需要在AndroidManifest.xml中加上权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
使用这些方法的时候直接调用就好了
返回结果:
//
https://github.com/sufadi/AndroidCpuTools
- C:\Users\fadi.su>adb shell cat /proc/gpufreq/gpufreq_opp_dump
- [0] freq = 450000, volt = 115000, idx = 0
- [1] freq = 400000, volt = 111875, idx = 1
- [2] freq = 350000, volt = 108750, idx = 2
- [3] freq = 290000, volt = 105000, idx = 3
-
- public static List getGpuFreqVolt() {
- List result = new ArrayList<>();
-
- try {
- String line;
- BufferedReader br = new BufferedReader(new FileReader("/proc/gpufreq/gpufreq_opp_dump"));
- while ((line = br.readLine()) != null) {
- result.add(line);
- }
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- return result;
- }
- final RendererUtil mRendererUtil = new RendererUtil();
- GLSurfaceView mGLSurfaceView = (GLSurfaceView) mView.findViewById(R.id.surfaceView);
- mGLSurfaceView.setEGLContextClientVersion(1);
- mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0);
- mGLSurfaceView.setRenderer(mRendererUtil);
- mGLSurfaceView.post(new Runnable() {
- @Override
- public void run() {
- result.add(getString(R.string.gl_vendor) + mRendererUtil.gl_vendor);
- result.add(getString(R.string.gl_renderer) + mRendererUtil.gl_renderer);
- result.add(getString(R.string.gl_version) + mRendererUtil.gl_version);
- result.add(getString(R.string.gl_extensions) + mRendererUtil.gl_extensions);
- mHandler.sendEmptyMessage(UPDATE_UI);
- }
- });
-
- package com.fadisu.cpurun.util;
-
- import android.opengl.GLSurfaceView;
- import android.util.Log;
-
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
-
- public class RendererUtil implements GLSurfaceView.Renderer {
-
- //GPU 渲染器
- public String gl_renderer;
-
- //GPU 供应商
- public String gl_vendor;
-
- //GPU 版本
- public String gl_version;
-
- //GPU 扩展名
- public String gl_extensions;
-
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
- gl_renderer = gl.glGetString(GL10.GL_RENDERER);
- gl_vendor = gl.glGetString(GL10.GL_VENDOR);
- gl_version = gl.glGetString(GL10.GL_VERSION);
- gl_extensions = gl.glGetString(GL10.GL_EXTENSIONS);
- Log.d("suhuazhi", "onSurfaceCreated = " + gl_renderer);
- }
-
- @Override
- public void onSurfaceChanged(GL10 gl10, int i, int i1) {
-
- }
-
- @Override
- public void onDrawFrame(GL10 gl10) {
-
- }
- }
- public class ScreenInfo {
-
- // 英寸
- public double size;
-
- public String sizeStr;
-
- // 高
- public int heightPixels;
-
- // 宽
- public int widthPixels;
-
- public String screenRealMetrics;
-
- // 显示器的逻辑密度
- public float density;
-
- // 屏幕密度为点每英寸
- public int densityDpi;
-
- public String densityDpiStr;
-
- // 显示在显示器的字体的定标因子
- public float scaledDensity;
-
- // 每在 X 维屏幕英寸的确切物理像素
- public float xdpi;
-
- // 每在 Y 维屏幕英寸的确切物理像素
- public float ydpi;
-
- //在屏幕中显示的参考密度
- public int density_default;
-
- @Override
- public String toString() {
- return "ScreenInfo{" +
- "size=" + size +
- ", sizeStr='" + sizeStr + '\'' +
- ", heightPixels=" + heightPixels +
- ", screenRealMetrics='" + screenRealMetrics + '\'' +
- ", widthPixels=" + widthPixels +
- ", density=" + density +
- ", densityDpi=" + densityDpi +
- ", densityDpiStr='" + densityDpiStr + '\'' +
- ", scaledDensity=" + scaledDensity +
- ", xdpi=" + xdpi +
- ", ydpi=" + ydpi +
- ", density_default=" + density_default +
- '}';
- }
- }
-
- /**
- * 屏幕分辨率
- *
- * @param mContext
- * @return
- */
- public static ScreenInfo getScreenInfo(Context mContext) {
- ScreenInfo result = new ScreenInfo();
- int widthPixels;
- int heightPixels;
-
- WindowManager w = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
-
- Display d = w.getDefaultDisplay();
- DisplayMetrics metrics = new DisplayMetrics();
- d.getMetrics(metrics);
- // since SDK_INT = 1;
- widthPixels = metrics.widthPixels;
- heightPixels = metrics.heightPixels;
- // includes window decorations (statusbar bar/menu bar)
- if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
- try {
- widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
- heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
- } catch (Exception ignored) {
- ignored.printStackTrace();
- }
- }
- // includes window decorations (statusbar bar/menu bar)
- if (Build.VERSION.SDK_INT >= 17) {
- try {
- Point realSize = new Point();
- Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
- widthPixels = realSize.x;
- heightPixels = realSize.y;
- } catch (Exception ignored) {
- ignored.printStackTrace();
- }
- }
-
- result.widthPixels = widthPixels;
- result.heightPixels = heightPixels;
- result.screenRealMetrics = widthPixels + " X " + heightPixels;
- result.density = metrics.density;
- result.density_default = metrics.DENSITY_DEFAULT;
- result.densityDpi = metrics.densityDpi;
- result.densityDpiStr = metrics.densityDpi + " dpi";
- result.scaledDensity = metrics.scaledDensity;
- result.xdpi = metrics.xdpi;
- result.ydpi = metrics.ydpi;
- result.size = (Math.sqrt(Math.pow(widthPixels, 2) + Math.pow(heightPixels, 2)) / metrics.densityDpi);
- result.sizeStr = String.format("%.2f", result.size) + mContext.getResources().getString(R.string.sys_inches_unit);
-
- return result;
- }
效果:
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信息需要通过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);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。