android 利用superuser编写使用root权限的app

在默认情况下,Android的java层apk是没有root权限的,如果apk需要root权限进行一些操作,如system目录下的文件读写、cpu频率的改变等,需要获取root权限。

非常幸运的是,国外的牛人写了获取root权限的工具,就是superuser,他主要由两部分组成:su和superuser.apk。su是真正起作用的命令,其他的程序通过它获取root权限,而superuser.apk是一个权限跟踪应用程序,主要记录那些apk程序需要获取root权限及root权限的授权。

su的源代码在github站点的仓库里,https://github.com/ChainsDD/su-binary.git 

superuser.apk的源代码在 https://github.com/ChainsDD/Superuser.git

大家可以下载下来仔细研究一下。


接下来具体讲解如何利用这两个工具来编写使用root权限的apk,通过设置cpu的governor,可以改变cpu的动态调频模式,但是 /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 这个sys接口的权限是:

-rw-rw-r--    1 0        1000          4096 Apr 13 15:41 scaling_governor

其他的用户只有只读权限,所以要访问该接口,我们需要在程序里获取root权限。


下面是我写的一个例子:

[java]  view plain copy
  1. package com.feng.setcpu;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.DataOutputStream;  
  5. import java.io.IOException;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. import android.app.Activity;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.view.View;  
  13. import android.widget.AdapterView;  
  14. import android.widget.AdapterView.OnItemSelectedListener;  
  15. import android.widget.ArrayAdapter;  
  16. import android.widget.Spinner;  
  17. import android.widget.TextView;  
  18. import android.widget.Toast;  
  19.   
  20. public class SetCPU extends Activity {  
  21.       
  22.     private final String TAG = "SetCPU";  
  23.     private List<String> governors;  
  24.       
  25.     private Spinner spinner;  
  26.     private ArrayAdapter<String> adapter;  
  27.     private TextView tv;  
  28.       
  29.     private int curCpuGovernor;  
  30.     private final String cpuFreqPath = "/sys/devices/system/cpu/cpu0/cpufreq";  
  31.     /** Called when the activity is first created. */  
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.           
  37.         governors = readCpuGovernors();  
  38.         curCpuGovernor = governors.indexOf(readCurCpuGovernor());  
  39.         spinner = (Spinner) findViewById(R.id.governors);  
  40.         tv = (TextView) findViewById(R.id.tv);  
  41.           
  42.         adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, governors);  
  43.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  44.         spinner.setAdapter(adapter);  
  45.         spinner.setPrompt("CPU Governors");  
  46.         spinner.setOnItemSelectedListener(new OnItemSelectedListener() {  
  47.   
  48.             @Override  
  49.             public void onItemSelected(AdapterView<?> parent, View view,  
  50.                     int position, long id) {  
  51.                 Log.i(TAG, "set CPU Governor " + readCurCpuGovernor() + "-> " + governors.get(position));  
  52.                   
  53.                 writeCpuGovernor(governors.get(position));  
  54.                 if(governors.get(position).equals(readCurCpuGovernor())){  
  55.                     Toast.makeText(SetCPU.this"write CPU Governor success!", Toast.LENGTH_LONG).show();  
  56.                     curCpuGovernor = governors.indexOf(readCurCpuGovernor());  
  57.                 }  
  58.                 else{  
  59.                     Toast.makeText(SetCPU.this"write CPU Governor failed!", Toast.LENGTH_LONG).show();  
  60.                     spinner.setSelection(curCpuGovernor);  
  61.                 }  
  62.             }  
  63.   
  64.             @Override  
  65.             public void onNothingSelected(AdapterView<?> parent) {  
  66.                   
  67.             }  
  68.         });  
  69.           
  70.           
  71.     }  
  72.       
  73.     private boolean writeCpuGovernor(String governor)  
  74.     {  
  75.         DataOutputStream os = null;  
  76.         byte[] buffer = new byte[256];  
  77.         String command = "echo " + governor  + " > " + cpuFreqPath + "/scaling_governor";  
  78.         Log.i(TAG, "command: " + command);  
  79.         try {  
  80.             Process process = Runtime.getRuntime().exec("su");  
  81.             os = new DataOutputStream(process.getOutputStream());  
  82.             os.writeBytes(command + "\n");  
  83.             os.writeBytes("exit\n");  
  84.             os.flush();  
  85.             process.waitFor();  
  86.             Log.i(TAG, "exit value = " + process.exitValue());  
  87.         } catch (IOException e) {  
  88.             Log.i(TAG, "writeCpuGovernor: write CPU Governor(" + governor + ") failed!");  
  89.             return false;  
  90.         } catch (InterruptedException e) {  
  91.             e.printStackTrace();  
  92.         }  
  93.         return true;  
  94.     }  
  95.       
  96.     private String readCurCpuGovernor()  
  97.     {  
  98.         String governor = null;  
  99.         DataInputStream is = null;  
  100.         try {  
  101.             Process process = Runtime.getRuntime().exec("cat " + cpuFreqPath + "/scaling_governor");  
  102.             is = new DataInputStream(process.getInputStream());  
  103.             governor = is.readLine();  
  104.         } catch (IOException e) {  
  105.             Log.i(TAG, "readCurCpuGovernor: read CPU Governor failed!");  
  106.             return null;  
  107.         }  
  108.         return governor;  
  109.     }  
  110.       
  111.     private List<String> readCpuGovernors()  
  112.     {  
  113.         List<String> governors = new ArrayList<String>();  
  114.         DataInputStream is = null;  
  115.         try {  
  116.             Process process = Runtime.getRuntime().exec("cat " + cpuFreqPath + "/scaling_available_governors");  
  117.             is = new DataInputStream(process.getInputStream());  
  118.             String line = is.readLine();  
  119.       
  120.             String[] strs = line.split(" ");  
  121.             for(int i = 0; i < strs.length; i++)  
  122.                 governors.add(strs[i]);  
  123.         } catch (IOException e) {  
  124.             Log.i(TAG, "readCpuGovernors: read CPU Governors failed!");  
  125.         }  
  126.         return governors;  
  127.     }  
  128. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值