Android app開發中獲取cpu arm架構信息及執行shell命令方法

    最近在做一個項目,需要在app開發過程中去判斷cpu的arm架構,比如說是armeabi-v7a,或是arm64-v8a。

    其實,在adb shell命令下面,可以通過getprop的方式,獲取到一些信息,比如:

rk3399_urbetter:/ # getprop|grep arm
[dalvik.vm.isa.arm.features]: [default]
[dalvik.vm.isa.arm.variant]: [cortex-a53.a57]
[dalvik.vm.isa.arm64.features]: [default]
[dalvik.vm.isa.arm64.variant]: [cortex-a53]
[persist.sys.alarm.fixed]: [300000]
[ro.config.alarm_alert]: [Alarm_Classic.ogg]
[ro.product.cpu.abi]: [arm64-v8a]
[ro.product.cpu.abilist]: [arm64-v8a,armeabi-v7a,armeabi]
[ro.product.cpu.abilist32]: [armeabi-v7a,armeabi]
[ro.product.cpu.abilist64]: [arm64-v8a]
rk3399_urbetter:/ #

   可以看到,現在cpu的架構是支持arm64-v8a。

    那麼,在app的開發過程中,是怎麼樣獲取到這個值呢。

    分成兩個層次來討論:

 1) native層

     在native層,可以通過property_get()函數來實現,比如:

    char value[PROPERTY_VALUE_MAX];
    property_get(EXIT_PROP_NAME, value, "0");

2) java層

    這次遇到的問題就是需要在java層去做這個事情。網絡上面有介紹可以使用System.getproperty()來達到這個目標,不過Java的System.getProperty得到null,這個讓我非常的鬱悶,找了不少方式也都沒有辦法解決。如果有人知道爲什麼返回null,幫忙說明下。後面我自己也看看源碼。

     後面我找到了個折中的解決方案,就是在java層執行shell 命令,直接通過getprop的shell命令來獲取到結果。

cpu_abi = mCMD.execCmd("getprop ro.product.cpu.abi");
public static String execCmd(String cmd) {
        DataOutputStream dos = null;
        String result = "";
        String lastline = " ";
        try {
            Process process = Runtime.getRuntime().exec(cmd);// 經過Root處理的android系統即有su命令
            //get the err line

            InputStream stderr = process.getErrorStream();
            InputStreamReader isrerr = new InputStreamReader(stderr);
            BufferedReader brerr = new BufferedReader(isrerr);

            //get the output line
            InputStream outs = process.getInputStream();
            InputStreamReader isrout = new InputStreamReader(outs);
            BufferedReader brout = new BufferedReader(isrout);
            String errline = null;


            // get the whole error message
            String  line = "";

            while ( (line = brerr.readLine()) != null)
            {
                result += line;
                result += "/n";
            }

            if( result != "" )
            {
                // put the result string on the screen
                Log.i(TAG," the str error message " + result.toString());
            }

            // get the whole standard output string
            while ( (line = brout.readLine()) != null)
            {
                lastline = line;
                result += line;
                result += "/n";
            }
            if( result != "" )
            {
                // put the result string on the screen
                Log.i(TAG," the standard output message " + lastline.toString());
            }
        }catch(Throwable t)
        {
            t.printStackTrace();
        }
        return lastline.toString();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章