by Runtime.getruntime (). Exec calls programs or scripts under the underlying Linux

Source: Internet
Author: User

Android runtime makes it possible to directly invoke executable programs or scripts under the underlying Linux

For example, write a test tool in Linux, directly compiled after the APK in the runtime to invoke

or write a script that calls directly from the APK, eliminating the middle or jni

At least the efficiency should be higher.

Code:

[Java] view plaincopy

  1. Public class Test extends Activity {

  2. TextView text;

  3. /** Called when the activity is first created. * /

  4. @Override

  5. Public void onCreate (Bundle savedinstancestate) {

  6. Super. OnCreate (savedinstancestate);

  7. Setcontentview (R.layout.main);

  8. Text = (TextView) Findviewbyid (R.id.text);

  9. Button Btn_ls = (button) Findviewbyid (R.id.btn_ls);

  10. Btn_ls.setonclicklistener (new Onclicklistener () {

  11. Public void OnClick (View v) {

  12. Do_exec ("Ls/mnt/sdcard");

  13. }

  14. });

  15. Button Btn_cat = (button) Findviewbyid (R.ID.BTN_CAT);

  16. Btn_cat.setonclicklistener (new Onclicklistener () {

  17. Public void OnClick (View v) {

  18. Do_exec ("cat/proc/version");

  19. }

  20. });

  21. Button Btn_rm = (button) Findviewbyid (R.ID.BTN_RM);

  22. Btn_rm.setonclicklistener (new Onclicklistener () {

  23. Public void OnClick (View v) {

  24. Do_exec ("rm/mnt/sdcard/1.jpg");

  25. }

  26. });

  27. Button Btn_sh = (button) Findviewbyid (R.ID.BTN_SH);

  28. Btn_sh.setonclicklistener (new Onclicklistener () {

  29. Public void OnClick (View v) {

  30. Do_exec ("/system/bin/sh/mnt/sdcard/test.sh 123");

  31. }

  32. });

  33. }

  34. String do_exec (string cmd) {

  35. String s = "/n";

  36. Try {

  37. Process p = runtime.getruntime (). exec (CMD);

  38. BufferedReader in = new BufferedReader (

  39. New InputStreamReader (P.getinputstream ()));

  40. String line = null;

  41. while (line = In.readline ()) = null) {

  42. s + = line + "/n";

  43. }

  44. } catch (IOException e) {

  45. //TODO auto-generated catch block

  46. E.printstacktrace ();

  47. }

  48. Text.settext (s);

  49. return cmd;

  50. }

  51. }

Test.sh:

Echo test.sh
echo $

Need to note:

1. exec does not equal the console command

2. exec's input/output stream needs to be handled by itself

3. Exec execution blocking, non-blocking, returning result problems

4. Attention to permissions issues


by Runtime.getruntime (). Exec calls programs or scripts under the underlying Linux

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.