Android uses root permissions to enable background simulation of global keys, touch-screen event methods (similar to Key wizard)

Source: Internet
Author: User

Continue to search the Internet Android button simulation (in fact, do not know what keyword is good, can think of the keywords are used all the time, but the results of the search, are mentioned before the several dependent on the source code environment and System permissions of the scheme). Discover a lot of articles that introduce ADB debugging and send key events to your phone. Just before, under root permission, use Java to invoke the Linux Shell on the bottom of the Android, and then execute PM instructions to load the APK. At this point, I have a whim, can I invoke the ADB command with the shell? The attempt was made to execute runtime.getruntime (). EXEC ("su") using Java. getoutputstream (), gets the output stream object of a process with root permission, To write a string to the root permission can be executed by the shell, the ADB analog key command is "input KeyEvent keycode", KeyCode is the key value of the key, such as KEYEVENT.KEYCODE_VOLUME_UP to indicate volume plus. After compiling the program installation execution, finally achieved the expected effect, was very happy. As for touch screen or mouse events, just call the appropriate ADB instructions. But a little bit of a problem is that the reaction speed is very slow, especially when the continuous simulation of multiple keys, even the crash. While the key wizard runs quite smoothly, I began to wonder how the key Wizard was implemented. And then finally found the reason, when the simulation button, should not be called every Time Runtime.getruntime (). EXEC ("su"), because every time this code is called, the runtime instance is obtained, and the "SU" request root permission is executed. The reaction will be slow (I understand that it is equivalent to opening a new command-line window every time), and should just be executed at the beginning and fetch an OutputStream instance, and then each time a shell instruction is executed, it is much quicker to write the corresponding string to it. The available code is posted below. Requires that the device is root and does not require any other special permissions or signatures. Because of the ADB directive, compatibility is not too much of a problem. The first time the program is run (in fact, when executing runtime.exec ("su"), the root permission is requested.
/** * Execute the shell command under Linux with root privileges *  * @author jzj * @since 2014-09-09 */public class Rootshellcmd {private OutputStream OS /** * Execute SHELL command *  * @param cmd *            instruction */public final void exec (String cmd) {try {if (OS = = null) {OS = Runtime.getr Untime (). EXEC ("su"). Getoutputstream ();} Os.write (Cmd.getbytes ()); Os.flush ();} catch (Exception e) {e.printstacktrace ();}} /** * Background emulation Global key *  * @param keycode *            key value */public final void simulatekey (int keycode) {exec ("input keyevent" + KEYC Ode + "\ n");}}

The main purpose of writing this article is not to emphasize the difficulty of the matter, nor just to propose a solution to the problem (that is, there is no need to write so many processes ahead). It's about trying to write the whole process of my problem-solving, which is a record for me, and for the reader, maybe I can find something. After solving the problem, it was discovered unexpectedly that someone had solved the problem and made a blog. Unfortunately, the blog was buried by a large number of blogs using the first two ideas, and I didn't find it. This blog address is here: http://blog.csdn.net/aminfo/article/details/7785975 by the way, the authors of this blog mention the disadvantages: slow response. As mentioned earlier, I have also been given a solution to the same problem. This article by jzj1993 Original, reprint please indicate source: http://www.hainter.com/android-key-simulation
(except for articles marked with the original link) Android analog key issues summary [using the Iwindowmanager.injectkeyevent method]http://blog.csdn.net/xudongdong99/article/ details/8857173 using the Hidden API (lots of illustrations) in Android http://163liufuliang.blog.163.com/blog/static/331651862013119114431760/

Through runtime implementation, the code is as follows:

[HTML]View Plaincopy
  1. Try
  2. {
  3. String Keycommand = "input keyevent" + keyevent.keycode_menu;
  4. Runtime runtime = runtime.getruntime ();
  5. Process proc = runtime.exec (keycommand);
  6. }
  7. catch (IOException E)
  8. {
  9. TODO auto-generated Catch block
  10. E.printstacktrace ();
  11. }

This code simulates the menu key and simulates other keys by simply replacing the keyevent.keycode_menu with other key values.

Cons: Slow reaction speed

The following KeyCode values are included:

[HTML]View Plaincopy
  1. keycode_unknown=0;
  2. keycode_soft_left=1;
  3. keycode_soft_right=2;
  4. Keycode_home=3;
  5. keycode_back=4;
  6. Keycode_call=5;
  7. Keycode_endcall=6;
  8. keycode_0=7;
  9. keycode_1=8;
  10. keycode_2=9;
  11. keycode_3=10;
  12. keycode_4=11;
  13. keycode_5=12;
  14. keycode_6=13;
  15. keycode_7=14;
  16. keycode_8=15;
  17. keycode_9=16;
  18. Keycode_star=17;
  19. keycode_pound=18;
  20. keycode_dpad_up=19;
  21. keycode_dpad_down=20;
  22. keycode_dpad_left=21;
  23. keycode_dpad_right=22;
  24. keycode_dpad_center=23;
  25. keycode_volume_up=24;
  26. keycode_volume_down=25;
  27. Keycode_power=26;
  28. Keycode_camera=27;
  29. Keycode_clear=28;
  30. keycode_a=29;
  31. keycode_b=30;
  32. keycode_c=31;
  33. Keycode_d=32;
  34. Keycode_e=33;
  35. keycode_f=34;
  36. keycode_g=35;
  37. Keycode_h=36;
  38. keycode_i=37;
  39. keycode_j=38;
  40. keycode_k=39;
  41. Keycode_l=40;
  42. keycode_m=41;
  43. keycode_n=42;
  44. keycode_o=43;
  45. keycode_p=44;
  46. keycode_q=45;
  47. Keycode_r=46;
  48. keycode_s=47;
  49. keycode_t=48;
  50. keycode_u=49;
  51. keycode_v=50;
  52. Keycode_w=51;
  53. Keycode_x=52;
  54. Keycode_y=53;
  55. keycode_z=54;
  56. Keycode_comma=55;
  57. Keycode_period=56;
  58. keycode_alt_left=57;
  59. Keycode_alt_right=58;
  60. keycode_shift_left=59;
  61. keycode_shift_right=60;
  62. keycode_tab=61;
  63. Keycode_space=62;
  64. keycode_sym=63;
  65. Keycode_explorer=64;
  66. Keycode_envelope=65;
  67. Keycode_enter=66;
  68. Keycode_del=67;
  69. Keycode_grave=68;
  70. keycode_minus=69;
  71. keycode_equals=70;
  72. keycode_left_bracket=71;
  73. keycode_right_bracket=72;
  74. Keycode_backslash=73;
  75. keycode_semicolon=74;
  76. Keycode_apostrophe=75;
  77. Keycode_slash=76;
  78. Keycode_at=77;
  79. keycode_num=78;
  80. keycode_headsethook=79;
  81. keycode_focus=80;//*camera*focus
  82. keycode_plus=81;
  83. keycode_menu=82;
  84. keycode_notification=83;
  85. Keycode_search=84;
  86. keycode_media_play_pause=85;
  87. keycode_media_stop=86;
  88. keycode_media_next=87;
  89. keycode_media_previous=88;
  90. keycode_media_rewind=89;
  91. Keycode_media_fast_forward=90;
  92. Keycode_mute=91;

Android uses root permissions to enable background simulation of global keys, touch-screen event methods (similar to Key wizard)

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.