美文网首页大数据 爬虫Python AI Sql
android app下运行linux脚本代码

android app下运行linux脚本代码

作者: 简书用户9527 | 来源:发表于2018-03-03 19:10 被阅读45次

    场景

    在@函兮的要求下,在手机root的情况下,完整的利用八核的性能,将linux代码写入到android 手机上,通过app的形式,进行测试。

    代码

    # 强制读写系统分区
    busybox mount -o rw,remount /dev/block/bootdevice/by-name/system
    # busybox mount -o rw,remount /dev/block/platform/msm_sdcc.1/by-name/system
    # 开始设置性能 当前模式:性能模式
    stop thermald
    stop mpdecision
    stop thermal-engine
    echo 0 > /sys/module/msm_thermal/core_control/enabled
    echo 1 > /sys/devices/system/cpu/cpu0/online
    echo 1 > /sys/devices/system/cpu/cpu1/online
    echo 1 > /sys/devices/system/cpu/cpu2/online
    echo 1 > /sys/devices/system/cpu/cpu3/online
    echo 1 > /sys/devices/system/cpu/cpu4/online
    echo 1 > /sys/devices/system/cpu/cpu5/online
    echo 1 > /sys/devices/system/cpu/cpu6/online
    echo 1 > /sys/devices/system/cpu/cpu7/online
    busybox echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
    busybox echo performance > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor
    busybox echo performance > /sys/devices/system/cpu/cpu4/cpufreq/scaling_governor
    busybox echo performance > /sys/devices/system/cpu/cpu5/cpufreq/scaling_governor
    busybox echo performance > /sys/devices/system/cpu/cpu7/cpufreq/scaling_governor
    echo performance > /sys/class/kgsl/kgsl-3d0/devfreq/governor
    # busybox echo 0 > /sys/devices/soc.0/fdb00000.qcom,kgsl-3d0/kgsl/kgsl-3d0/min_pwrlevel
    # busybox echo 4 > /sys/devices/system/cpu/cpu4/core_ctl/min_cpus
    # 缓存目录权限应当为777
    busybox chmod -R 777 /data/local/tmp/
    tmp=/data/local/tmp/80rompk_error.txt
    
    

    工具类的代码:

    public final class RootCmd
    {
      //执行linux命令并且输出结果
      protected static Vector execRootCmd(String paramString)
      {
        Vector localVector = new Vector();
        try
        {
          Process localProcess = Runtime.getRuntime().exec("su ");//经过Root处理的android系统即有su命令
          OutputStream localOutputStream = localProcess.getOutputStream();
          DataOutputStream localDataOutputStream = new DataOutputStream(localOutputStream);
          InputStream localInputStream = localProcess.getInputStream();
          DataInputStream localDataInputStream = new DataInputStream(localInputStream);
          String str1 = String.valueOf(paramString);
          String str2 = str1 + "\n";
          localDataOutputStream.writeBytes(str2);
          localDataOutputStream.flush();
          String str3 = localDataInputStream.readLine();
          localVector.add(str3);
          localDataOutputStream.writeBytes("exit\n");
          localDataOutputStream.flush();
          localProcess.waitFor();
          return localVector;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
        }
      }
     
       //执行linux命令但不关注结果输出
      protected static int execRootCmdSilent(String paramString)
      {
        try
        {
          Process localProcess = Runtime.getRuntime().exec("su");
          Object localObject = localProcess.getOutputStream();
          DataOutputStream localDataOutputStream = new DataOutputStream((OutputStream)localObject);
          String str = String.valueOf(paramString);
          localObject = str + "\n";
          localDataOutputStream.writeBytes((String)localObject);
          localDataOutputStream.flush();
          localDataOutputStream.writeBytes("exit\n");
          localDataOutputStream.flush();
          localProcess.waitFor();
          localObject = localProcess.exitValue();
          return localObject;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
        }
      }
     
     //判断机器Android是否已经root,即是否获取root权限
      protected static boolean haveRoot()
      {
    
    
        int i = execRootCmdSilent("echo test"); //通过执行测试命令来检测
        if (i != -1)  return true;
        retrun false;
      }
     
    }
    

    这段代码直接在onCreate()` 里面直接调用即可。

    相关文章

      网友评论

        本文标题:android app下运行linux脚本代码

        本文链接:https://www.haomeiwen.com/subject/okhxfftx.html