中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android獲取手機的相關(guān)硬件信息的代碼

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
import android.app.ActivityManager;  
import android.content.Context;  
import android.os.Bundle;  
import android.support.v7.app.ActionBarActivity;  
import android.telephony.TelephonyManager;  
import android.text.format.Formatter;  
import android.util.Log;  
import android.widget.TextView;  
  
import java.io.BufferedReader;  
import java.io.FileNotFoundException;  
import java.io.FileReader;  
import java.io.IOException;  
import java.io.InputStream;  
  
public class MainActivity extends ActionBarActivity {  
  
    private TextView mTextView;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        mTextView = (TextView) findViewById(R.id.textViewId);  
        getPhoneInfo();  
    }  
  
    /** 
     * 獲取手機信息 
     */  
    public void getPhoneInfo() {  
        TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);  
        String mtyb = android.os.Build.BRAND;// 手機品牌  
        String mtype = android.os.Build.MODEL; // 手機型號  
        String imei = tm.getDeviceId();  
        String imsi = tm.getSubscriberId();  
        String numer = tm.getLine1Number(); // 手機號碼  
        String serviceName = tm.getSimOperatorName(); // 運營商  
        mTextView.setText("品牌: " + mtyb + "\n" + "型號: " + mtype + "\n" + "版本: Android "  
                + android.os.Build.VERSION.RELEASE + "\n" + "IMEI: " + imei  
                + "\n" + "IMSI: " + imsi + "\n" + "手機號碼: " + numer + "\n"  
                + "運營商: " + serviceName + "\n");  
        mTextView.append("總內(nèi)存: " + getTotalMemory() + "\n");  
        mTextView.append("當(dāng)前可用內(nèi)存: " + getAvailMemory() + "\n");  
        mTextView.append("CPU名字: " + getCpuName() + "\n");  
        mTextView.append("CPU最大頻率: " + getMaxCpuFreq() + "\n");  
        mTextView.append("CPU最小頻率: " + getMinCpuFreq() + "\n");  
        mTextView.append("CPU當(dāng)前頻率: " + getCurCpuFreq() + "\n");  
    }  
  
    /** 
     * 獲取手機內(nèi)存大小 
     * 
     * @return 
     */  
    private String getTotalMemory() {  
        String str1 = "/proc/meminfo";// 系統(tǒng)內(nèi)存信息文件  
        String str2;  
        String[] arrayOfString;  
        long initial_memory = 0;  
        try {  
            FileReader localFileReader = new FileReader(str1);  
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);  
            str2 = localBufferedReader.readLine();// 讀取meminfo第一行,系統(tǒng)總內(nèi)存大小  
  
            arrayOfString = str2.split("\\s+");  
            for (String num : arrayOfString) {  
                Log.i(str2, num + "\t");  
            }  
  
            initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 獲得系統(tǒng)總內(nèi)存,單位是KB,乘以1024轉(zhuǎn)換為Byte  
            localBufferedReader.close();  
  
        } catch (IOException e) {  
        }  
        return Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte轉(zhuǎn)換為KB或者M(jìn)B,內(nèi)存大小規(guī)格化  
    }  
  
    /** 
     * 獲取當(dāng)前可用內(nèi)存大小 
     * 
     * @return 
     */  
    private String getAvailMemory() {  
        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);  
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();  
        am.getMemoryInfo(mi);  
        return Formatter.formatFileSize(getBaseContext(), mi.availMem);  
    }  
  
    public static String getMaxCpuFreq() {  
        String result = "";  
        ProcessBuilder cmd;  
        try {  
            String[] args = {"/system/bin/cat",  
                    "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"};  
            cmd = new ProcessBuilder(args);  
            Process process = cmd.start();  
            InputStream in = process.getInputStream();  
            byte[] re = new byte[24];  
            while (in.read(re) != -1) {  
                result = result + new String(re);  
            }  
            in.close();  
        } catch (IOException ex) {  
            ex.printStackTrace();  
            result = "N/A";  
        }  
        return result.trim() + "Hz";  
    }  
  
    // 獲取CPU最小頻率(單位KHZ)  
  
    public static String getMinCpuFreq() {  
        String result = "";  
        ProcessBuilder cmd;  
        try {  
            String[] args = {"/system/bin/cat",  
                    "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"};  
            cmd = new ProcessBuilder(args);  
            Process process = cmd.start();  
            InputStream in = process.getInputStream();  
            byte[] re = new byte[24];  
            while (in.read(re) != -1) {  
                result = result + new String(re);  
            }  
            in.close();  
        } catch (IOException ex) {  
            ex.printStackTrace();  
            result = "N/A";  
        }  
        return result.trim() + "Hz";  
    }  
  
    // 實時獲取CPU當(dāng)前頻率(單位KHZ)  
  
    public static String getCurCpuFreq() {  
        String result = "N/A";  
        try {  
            FileReader fr = new FileReader(  
                    "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");  
            BufferedReader br = new BufferedReader(fr);  
            String text = br.readLine();  
            result = text.trim() + "Hz";  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return result;  
    }  
  
    public static String getCpuName() {  
        try {  
            FileReader fr = new FileReader("/proc/cpuinfo");  
            BufferedReader br = new BufferedReader(fr);  
            String text = br.readLine();  
            String[] array = text.split(":\\s+", 2);  
            for (int i = 0; i < array.length; i++) {  
            }  
            return array[1];  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
}  

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:打開或關(guān)閉軟鍵盤Android工具類

下一篇:不依賴于任何Java類庫的中文轉(zhuǎn)拼音實現(xiàn)