加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:28:44 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import android.app.ActivityManager; import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; i

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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);  
    }  
}  

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读