Android获取Build和内存,分辨率等信息

package com.zms.getdeviceinfo;

import android.app.ActivityManager;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.TimeZone;

public class Main extends ActionBarActivity {
    private TextView tvInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tvInfo = (TextView) findViewById(R.id.tvInfo);
        tvInfo.setText(getBuildInfo(0) + getDisplayInfo(0) + getTelephonyState(0) + getTimeZoneInfo()
                + getHardwareInfo(0));
    }

    /**
     * @param type 0-All,1-Model,2-VersionRelease,Other-Error.
     * @return String
     */
    private String getBuildInfo(int type) {
        String model = Build.MODEL; // 型号
        String versionRelease = Build.VERSION.RELEASE;
        String versionSDK = Build.VERSION.SDK;
        String versionIncremental = Build.VERSION.INCREMENTAL;
        String brand = Build.BRAND;
        String id = Build.ID;
        String manufacturer = Build.MANUFACTURER;
        String hardware = Build.HARDWARE;
        String product = Build.PRODUCT;
        String cpuABI = Build.CPU_ABI;
        String cpuABI2 = Build.CPU_ABI2;
        String fingerPrint = Build.FINGERPRINT;
        String board = Build.BOARD;
        String serial = Build.SERIAL;
        String user = Build.USER;
        if (type == 0) {
            return "Model:" + model + "\nVersionRelease:" + versionRelease + "\nVersionSDK:" +
                    versionSDK + "\nVersionIncremental:" + versionIncremental + "\nBrand:" + brand
                    + "\nID:" + id + "\nManufacturer:" + manufacturer + "\nHardware:" + hardware
                    + "\nProduct:" + product + "\nCpuABI:" + cpuABI + "\nCpuABI2:" + cpuABI2 +
                    "\nFingerPrint:" + fingerPrint + "\nBoard:" + board + "\nSerial:" + serial +
                    "\nUser:" + user + "\n";
        } else if (type == 1) {
            return "Model" + model + "\n";
        } else if (type == 2) {
            return "VersionRelease:" + versionRelease + "\n";
        } else {
            return "[getBuildInfo Err]";
        }
    }

    private String getHardwareInfo(int type) {

        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        String macAddress = wifiInfo.getMacAddress();

        String strCPU = "";
        String[] cpuInfo = {"", ""};  //1-cpu型号  //2-cpu频率
        String[] arrayOfStringCPU;
        try {
            FileReader fileReader = new FileReader("/proc/cpuinfo");
            BufferedReader localBufferedReader = new BufferedReader(fileReader, 8192);
            strCPU = localBufferedReader.readLine();
            arrayOfStringCPU = strCPU.split("\\s+");
            for (int i = 2; i < arrayOfStringCPU.length; i++) {
                cpuInfo[0] = cpuInfo[0] + arrayOfStringCPU[i] + " ";
            }
            strCPU = localBufferedReader.readLine();
            arrayOfStringCPU = strCPU.split("\\s+");
            cpuInfo[1] += arrayOfStringCPU[2];
            localBufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // adb shell;cat /proc/meminfo
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        ActivityManager activityManager = (ActivityManager) this.getSystemService(
                Context.ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(memoryInfo);
        long mTotalMem = memoryInfo.totalMem;//0;
        long mAvailMem = memoryInfo.availMem;
        /*
        String strMemory;
        String[] arrayOfStringMemory;
        try {
            FileReader localFileReader = new FileReader("/proc/meminfo");
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
            strMemory = localBufferedReader.readLine();
            arrayOfStringMemory = strMemory.split("\\s+");
            mTotalMem = Integer.valueOf(arrayOfStringMemory[1]).intValue() * 1024;
            localBufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        */
        String totalMemory = Formatter.formatFileSize(this, mTotalMem);
        String availMemory = Formatter.formatFileSize(this, mAvailMem);

        if (type == 0) {
            return "MacAddress:" + macAddress + "\nCpuModel:" + cpuInfo[0] + "\nCpuClock:" +
                    cpuInfo[1] + "\nTotalMemory:" + totalMemory + "\nAvailableMemory:" + availMemory
                    + "\n";
        } else {
            return "";
        }
    }

    /**
     * @param type 0-All,1-DeviceId,2-SubscriberId,3-PhoneNumber,Other-Error.
     * @return String
     */
    private String getTelephonyState(int type) {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        String deviceId = telephonyManager.getDeviceId();
        String subscriberId = telephonyManager.getSubscriberId();
        String phoneNumber = telephonyManager.getLine1Number();

        if (type == 0) {
            return "DeviceId:" + deviceId + "\nSubscriberId:" + subscriberId + "\nPhoneNumber:" + phoneNumber + "\n";
        } else if (type == 1) {
            return "DeviceId:" + deviceId + "\n";
        } else if (type == 2) {
            return "SubscriberId" + subscriberId + "\n";
        } else if (type == 3) {
            return "PhoneNumber:" + phoneNumber + "\n";
        } else {
            return "[getTelephonyState Err]";
        }
    }

    /**
     * @param type 0-All,1-ScreenHeight,2-ScreenWidth,Other-Error.
     * @return String
     */
    private String getDisplayInfo(int type) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int screenHeight = displayMetrics.heightPixels;
        int screenWidth = displayMetrics.widthPixels;
        if (type == 0) {
            return "ScreenHeight:" + screenHeight + "\nScreenWidth:" + screenWidth + "\n";
        } else if (type == 1) {
            return "ScreenHeight:" + screenHeight + "\n";
        } else if (type == 2) {
            return "ScreenWidth:" + screenWidth + "\n";
        } else {
            return "[getTelephonyState Err]";
        }
    }

    private String getTimeZoneInfo() {
        TimeZone timeZone = TimeZone.getDefault();
        return "TimeZone:" + timeZone.toString() + "\n";
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
时间: 2024-10-24 05:51:22

Android获取Build和内存,分辨率等信息的相关文章

Android获取cpu和内存信息、网址的代码

android获取手机cpu并判断是单核还是多核 /** * Gets the number of cores available in this device, across all processors. * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu" * @return The number of cores, or 1 if failed to get result */ pri

Android获取第三方闹钟的闹铃信息

闹铃信息采集 闹铃时间,闹铃备注信息 闹铃引起系统变化的点: 1. Send Notification 2. Play Ring 闹铃信息结构体 ClockInfo{ String apkName; String startTime; String backup; boolean isPlayingRing; } SendNotification SystemUI BaseStatusBar.java 解析ExpandedView,获取Clock发送的Notification.因为在BaseSt

Android 获取手机总内存和可用内存等信息

在android开发中,有时候我们想获取手机的一些硬件信息,比如android手机的总内存和可用内存大小.这个该如何实现呢? 通过读取文件"/proc/meminfo"的信息能够获取手机Memory的总量,而通过ActivityManager.getMemoryInfo(ActivityManager.MemoryInfo)方法可以获取当前的可用Memory量. "/proc/meminfo"文件记录了android手机的一些内存信息,在命令行窗口里输入"

Android获取手机和系统版本等信息的代码

有时候需要统计手机的型号和版本号,利用程序可以获取到相应的手机信息,对比两部手机发现,厂商不同,某个信息显示方式也不尽相同,具体见: 1 String phoneInfo = "Product: " + android.os.Build.PRODUCT; 2 phoneInfo += ", CPU_ABI: " + android.os.Build.CPU_ABI; 3 phoneInfo += ", TAGS: " + android.os.B

java获取cpu、内存、硬盘信息

1 下载安装sigar-1.6.4.zip 使用java自带的包获取系统数据,容易找不到包,尤其是内存信息不够准确,所以选择使用sigar获取系统信息. 下载地址:http://sourceforge.net/projects/sigar/files/latest/download?source=files 解压压缩包,将lib下sigar.jar导入eclipse的CLASSPATH中,再将sigar-x86-winnt.dll存入Java的bin目录即可. 2 代码示例 /**    * @

android 获取 imei号码 以及其他手机信息

核心代码: Imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)) .getDeviceId(); 1.加入权限 在manifest.xml文件中要添加 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 2.代码 /** * * *创建日期 2010-4-29 下午05:02:47 * */ package ne

安卓获取屏幕宽高/分辨率相关信息的方式

private void getSystemInfo() { //第一种方式获取屏幕的像素宽高在Activity里面可使用 //DisplayMetrics metric = new DisplayMetrics(); //Activity.getWindowManager().getDefaultDisplay().getMetrics(metric); //第二种方式获取屏幕的像素宽高 //获取屏幕的像素高度PIX screenHeight = getResources().getDispl

Android获取当前可用内存

写入本地文件 private void getMemo(){ //获得ActivityManager服务的对象 ActivityManager mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); //获得MemoryInfo对象 ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo() ; //获得系统

android 获取屏幕尺寸,密度等信息

摘选自 http://blog.csdn.net/chenguang79/article/details/9222817 DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0) int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:12