Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute shell command from android

I'm trying to execute this command from the application emulator terminal (you can find it in google play) in this app i write su and press enter, so write:

screenrecord --time-limit 10 /sdcard/MyVideo.mp4

and press again enter and start the recording of the screen using the new function of android kitkat.

so, i try to execute the same code from java using this:

Process su = Runtime.getRuntime().exec("su"); Process execute = Runtime.getRuntime().exec("screenrecord --time-limit 10 /sdcard/MyVideo.mp4"); 

But don't work because the file is not created. obviously i'm running on a rooted device with android kitkat installed. where is the problem? how can i solve? because from terminal emulator works and in Java not?

like image 501
Giovanni Mariotti Avatar asked Jan 05 '14 09:01

Giovanni Mariotti


People also ask

Can you use a shell in Android?

Open the Command Shell on an Remote Endpoint Using the Android Access Console. Remote command shell enables privileged users to open a virtual command line interface on remote computers. Users can then type locally but have the commands executed on the remote system. You can work from multiple shells.

What is shell command in Android?

During VTS testing, shell commands are used to execute a target-side test binary, to get/set properties, environment variables, and system information, and to start/stop the Android framework.

How do I execute a shell command in Kotlin?

Start by creating a Shell: val shell = Shell("sh") ``` Then invoke the `run()` method passing in shell command you want executed as a string: ```kotlin val result = shell. run("echo 'Hello, World! '")


1 Answers

You should grab the standard input of the su process just launched and write down the command there, otherwise you are running the commands with the current UID.

Try something like this:

try{     Process su = Runtime.getRuntime().exec("su");     DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());      outputStream.writeBytes("screenrecord --time-limit 10 /sdcard/MyVideo.mp4\n");     outputStream.flush();      outputStream.writeBytes("exit\n");     outputStream.flush();     su.waitFor(); }catch(IOException e){     throw new Exception(e); }catch(InterruptedException e){     throw new Exception(e); } 
like image 124
Carlo Cannas Avatar answered Sep 22 '22 01:09

Carlo Cannas