get Device Info - Android Hardware

Android examples for Hardware:Device Feature

Description

get Device Info

Demo Code


//package com.java2s;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import android.content.Context;

import android.os.Build;

import android.view.Display;
import android.view.WindowManager;

public class Main {
    public static String getDeviceInfo(Context context) {
        String DeviceInfo = "";
        DeviceInfo += "br:" + Build.BRAND.replace(" ", "");
        DeviceInfo += ";md:" + Build.MODEL.replace(" ", "");
        DeviceInfo += ";sver:" + Build.VERSION.RELEASE;
        DeviceInfo += ";res:"
                + getResolutionAsString(context.getApplicationContext());
        DeviceInfo += ";cpu:" + getCpuFre();

        return DeviceInfo;
    }//from   w ww. j  a  v a  2s.c o m

    public static String getResolutionAsString(Context context) {
        int widthPixels = 0;
        int heightPixels = 0;
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        if (Build.VERSION.SDK_INT <= 12) {
            widthPixels = display.getWidth();
            heightPixels = display.getHeight();
        } else {
            try {
                Method methodW = Display.class.getMethod("getRawWidth");
                widthPixels = (Integer) methodW.invoke(display);

                Method methodH = Display.class.getMethod("getRawHeight");
                heightPixels = (Integer) methodH.invoke(display);
            } catch (Exception e) {
                e.printStackTrace();
                widthPixels = display.getWidth();
                heightPixels = display.getHeight();
            }
        }
        return widthPixels < heightPixels ? widthPixels + "x"
                + heightPixels : heightPixels + "x" + widthPixels;
    }

    public static String getCpuFre() {
        String cpuFreFile = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
        return ((Long) readLong(cpuFreFile)).toString();
    }

    private static long readLong(String file) {
        RandomAccessFile raf = null;

        try {
            raf = getFile(file);
            return Long.valueOf(raf.readLine());
        } catch (Exception e) {
            return 0;
        } finally {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                }
            }
        }
    }

    private static RandomAccessFile getFile(String filename)
            throws IOException {
        File f = new File(filename);
        return new RandomAccessFile(f, "r");
    }
}

Related Tutorials