Call shell commands and scripts in android

Source: Internet
Author: User
Tags call shell

Call shell commands and scripts in android

A function of android recently called shell commands to perform system-level operations, such as shutdown and startup, now let's summarize the specific usage and pitfalls (based on what I used, I won't talk about what I didn't use)


(1)

Runtime.getRuntime().exec("ls");

This is the simplest one. You can run the ls command after entering it. If you want to obtain the output, you can write it like this.

Process p = Runtime.getRuntime().exec("ls");                String data = null;                BufferedReader ie = new BufferedReader(new InputStreamReader(p.getErrorStream()));                BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));                String error = null;                while ((error = ie.readLine()) != null                       && !error.equals("null")) {                    data += error + "\n";                }                String line = null;                while ((line = in.readLine()) != null                       && !line.equals("null")) {                    data += line + "\n";                }                Log.v("ls", data);


(2)

However, what I want to write must be executed with the root permission. For example, if the reboot command is restarted and executed with a normal identity, it will not succeed and permission denyed will be reported, so how can we execute it? Everyone knows about adb shell. Generally, all the hosts that run the root user can use su to obtain the Administrator permission, but they won't be able to execute it without the root user, this requires the host to be root.

Run the following command after root:

Process proc = Runtime.getRuntime().exec(new String[]{"su", reboot});

But !!!! This command does not seem to be available to all machines. I encountered such a pitfall. From the error message, we can see that su and reboot are connected and executed, therefore, it will be parsed to su reboot, which will cause a problem. After su and reboot are added, reboot will be parsed to su command parameters, let's take a look at su-help to see all the su parameters. Obviously, we can see that reboot cannot be directly followed by su, so I found a command that can be run as follows:


Process proc = Runtime.getRuntime().exec(su -c reboot);

In this way, the sub-host can execute the restart command with the root permission.


(3)

But when we encounter such a demand! The shell command to be run is as follows:

Process proc = Runtime.getRuntime().exec(su -c "ls /data");

After testing, although the commands in this code can run perfectly in the adb shell, the java program in android is paralyzed... In the android program, it seems that/data will be treated as a part of su parameter, which leads to command execution errors. However, after a long time, the implementation method is changed, the fourth method is used for implementation.


(4)

The fourth method is helpless. Anyone who has better suggestions is welcome.

This method is to write the command to be executed into a shell script, and then call this shell script in the program,

I put the command as a shell script in the mobile phone directory, and then the call is successful as follows.


 Process proc = Runtime.getRuntime().exec("su -s sh  -c /data/initcommand.sh");

Note: This is the same as the above problem. If the-c parameter is not Gary, the following file will still be treated as the su parameter. Of course,-c can also be changed to-s, in this way, the initcommand can be executed. sh script. If not, change the script to the 777 permission.

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.