现在的位置: 首页 > 综合 > 正文

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

2013年03月29日 ⁄ 综合 ⁄ 共 3907字 ⁄ 字号 评论关闭

在默认情况下,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权限。

下面是我写的一个例子:

package com.feng.setcpu;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SetCPU extends Activity {
	
	private final String TAG = "SetCPU";
	private List<String> governors;
	
	private Spinner spinner;
	private ArrayAdapter<String> adapter;
	private TextView tv;
	
	private int curCpuGovernor;
	private final String cpuFreqPath = "/sys/devices/system/cpu/cpu0/cpufreq";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        governors = readCpuGovernors();
        curCpuGovernor = governors.indexOf(readCurCpuGovernor());
        spinner = (Spinner) findViewById(R.id.governors);
        tv = (TextView) findViewById(R.id.tv);
        
        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, governors);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setPrompt("CPU Governors");
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				Log.i(TAG, "set CPU Governor " + readCurCpuGovernor() + "-> " + governors.get(position));
				
				writeCpuGovernor(governors.get(position));
				if(governors.get(position).equals(readCurCpuGovernor())){
					Toast.makeText(SetCPU.this, "write CPU Governor success!", Toast.LENGTH_LONG).show();
					curCpuGovernor = governors.indexOf(readCurCpuGovernor());
				}
				else{
					Toast.makeText(SetCPU.this, "write CPU Governor failed!", Toast.LENGTH_LONG).show();
					spinner.setSelection(curCpuGovernor);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
        
        
    }
    
    private boolean writeCpuGovernor(String governor)
    {
    	DataOutputStream os = null;
    	byte[] buffer = new byte[256];
    	String command = "echo " + governor  + " > " + cpuFreqPath + "/scaling_governor";
    	Log.i(TAG, "command: " + command);
    	try {
    		Process process = Runtime.getRuntime().exec("su");
	        os = new DataOutputStream(process.getOutputStream());
	        os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
	        Log.i(TAG, "exit value = " + process.exitValue());
		} catch (IOException e) {
			Log.i(TAG, "writeCpuGovernor: write CPU Governor(" + governor + ") failed!");
			return false;
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    	return true;
    }
    
    private String readCurCpuGovernor()
    {
    	String governor = null;
    	DataInputStream is = null;
    	try {
			Process process = Runtime.getRuntime().exec("cat " + cpuFreqPath + "/scaling_governor");
	        is = new DataInputStream(process.getInputStream());
			governor = is.readLine();
		} catch (IOException e) {
			Log.i(TAG, "readCurCpuGovernor: read CPU Governor failed!");
			return null;
		}
    	return governor;
    }
    
    private List<String> readCpuGovernors()
    {
    	List<String> governors = new ArrayList<String>();
    	DataInputStream is = null;
    	try {
    		Process process = Runtime.getRuntime().exec("cat " + cpuFreqPath + "/scaling_available_governors");
	        is = new DataInputStream(process.getInputStream());
			String line = is.readLine();
    
			String[] strs = line.split(" ");
			for(int i = 0; i < strs.length; i++)
				governors.add(strs[i]);
		} catch (IOException e) {
			Log.i(TAG, "readCpuGovernors: read CPU Governors failed!");
		}
    	return governors;
    }
}

抱歉!评论已关闭.