Android uses superuser to write an app with root permission and modify the CPU Governor

Source: Internet
Author: User

By default, the android Java-layer APK does not have the root permission. If the APK requires the root permission to perform some operations, such as file read/write under the system directory and CPU frequency change, you must obtain the root permission.

Fortunately, the foreign cool-man writes the tool for obtaining root permissions, namely, superuser, which is mainly composed of two parts: suhe and superuser.apk. Suis the starting command, and other programs get the rootpermission through the token, while superuser.apk is a permission tracking application, mainly recording the APK programs need to get the root permission and root permission authorization.

1) Su source code in the GitHub site repository, https://github.com/ChainsDD/su-binary.git

2) superuser.apk source code in https://github.com/ChainsDD/Superuser.git

You can download it and study it carefully.


Next, we will explain how to use these two tools to write an APK with the root permission. By setting the governor of the CPU, we can change the dynamic frequency modulation mode of the CPU, however, the permission for the Sys interface/sys/devices/system/CPU/cpu0/cpufreq/scaling_governor is:

-RW-r -- 1 0 1000 4096 APR 13 scaling_governor

Other users only have the read-only permission. to access this interface, we need to obtain the root permission in the program.


The following is an example:

01.package com.feng.setcpu;  02.  03.import java.io.DataInputStream;  04.import java.io.DataOutputStream;  05.import java.io.IOException;  06.import java.util.ArrayList;  07.import java.util.List;  08.  09.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.}  

From: http://blog.csdn.net/fengyanhui/article/details/7459639

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.