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

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android应用执行Shell命令可以通过`Runtime.getRuntime().exec(command)`方法来实现,其`command`是要执行Shell命令字符串,比如`am start -n com.example.app/.MainActivity`。 要实现从一个Android应用跳转到另一个应用,可以通过调用Shell命令执行`am start`命令。`am start`命令用于启动一个指定应用的特定Activity。 首先,需要确保设备已经root或者应用拥有相应的系统权限,才能执行Shell命令。然后,在应用通过`Runtime.getRuntime().exec()`方法执行Shell命令。 下面是一个示例代码: ```java try { // 构建要执行命令 String packageName = "com.example.app"; String activityName = "com.example.app.MainActivity"; String command = "am start -n " + packageName + "/" + activityName; // 执行Shell命令 Process process = Runtime.getRuntime().exec(command); // 读取命令结果 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { Log.d("Shell", line); } // 等待命令执行完成 process.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } ``` 上述代码,将要跳转的应用的包名和Activity名拼接成一个命令字符串,然后通过`Runtime.getRuntime().exec()`方法执行Shell命令。读取命令结果可以通过`InputStreamReader`和`BufferedReader`来实现,可以根据需要处理命令输出的结果。最后使用`process.waitFor()`等待命令执行完成。 需要注意的是,执行Shell命令需要小心处理,确保没有安全隐患。同时,要避免滥用Shell命令,以免影响到设备的正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值