简体   繁体   中英

How to execute linux command in android programmatically?

As in android(through android sdk/tools folder) from command line we can execute linux shell command to access mnt folder/data folder likewise. (eg cd data ls) now that command i want to execute from programmatically in android so how could it be possible?


I am using following code to execute shell command

java.io.IOException: Error running exec(). Command: [cd, data] Working 
Directory:  null Environment: null

but it is giving me exception

java.io.IOException: Error running exec(). Command: [cd, data] Working Directory: null Environment: null


so how should i proceed for it.Thanks in advance.

cd is not a Linux command, it's a command built into the shell; it changes the current working directory in the context of that shell process . In your case, if the command were to be successful, it would be successful for the child process only (which would soon terminate) and would have no effect on your own process.

试试这个 :

Process p = Runtime.getRuntime().exec("cd /data");

To retrieve the path to your app's private data folder use the following from Java:

File MyData = Ctxt.getDir("Foo");

Where Ctxt is a Context object, like an Activity. It will return you a path like /data/data/com.activity.networkRequestDetector/app_Foo . Note that reading/writing /data/data/com.activity.networkRequestDetector/ is discouraged in Android - it's your application's sandbox's root, not to be played with.

To open files from the data folder, use something like this:

FileInputStream Stm = new FileInputStream(new File(MyData, "Filename.txt"));

In general, anything a shell command does your app can do, too. Shell commands are just programs that use API like everyone else.

On Android, your process does not have permissions to read files in other app's /data/data/_other-package-name_ , or list its private files in directory /data/data/_other-package-name_/files . But it does have permission to list and read files in the lib directory /data/data/_other-package-name_/lib , and you can look at a specific file in /data/data/_other-package-name_/files , if the other-package opened this file as public.

Ie if the other-package does something in line with:

FileOutputStream fos = openFileOutput("public_file", Context.MODE_WORLD_READABLE);
fos.write("hello world".getBytes());
fos.close();

then your package can read this file like this:

byte[] bytes = new byte[100];
FileInputStream fis = new FileInputStream(new File("/data/data/*other-package*/files/public_file"));
int cnt = fis.read(bytes);
fis.close();
Log.d("Two_Libs", new String(bytes, 0, cnt));

But you cannot list the public files in that directory to discover them.

Use as following:-

 Process process = Runtime.getRuntime().exec("command to be executed");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM