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
- Try
- {
- String Keycommand = "input keyevent" + keyevent.keycode_menu;
- Runtime runtime = runtime.getruntime ();
- Process proc = runtime.exec (keycommand);
- }
- catch (IOException E)
- {
- TODO auto-generated Catch block
- E.printstacktrace ();
- }
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
- keycode_unknown=0;
- keycode_soft_left=1;
- keycode_soft_right=2;
- Keycode_home=3;
- keycode_back=4;
- Keycode_call=5;
- Keycode_endcall=6;
- keycode_0=7;
- keycode_1=8;
- keycode_2=9;
- keycode_3=10;
- keycode_4=11;
- keycode_5=12;
- keycode_6=13;
- keycode_7=14;
- keycode_8=15;
- keycode_9=16;
- Keycode_star=17;
- keycode_pound=18;
- keycode_dpad_up=19;
- keycode_dpad_down=20;
- keycode_dpad_left=21;
- keycode_dpad_right=22;
- keycode_dpad_center=23;
- keycode_volume_up=24;
- keycode_volume_down=25;
- Keycode_power=26;
- Keycode_camera=27;
- Keycode_clear=28;
- keycode_a=29;
- keycode_b=30;
- keycode_c=31;
- Keycode_d=32;
- Keycode_e=33;
- keycode_f=34;
- keycode_g=35;
- Keycode_h=36;
- keycode_i=37;
- keycode_j=38;
- keycode_k=39;
- Keycode_l=40;
- keycode_m=41;
- keycode_n=42;
- keycode_o=43;
- keycode_p=44;
- keycode_q=45;
- Keycode_r=46;
- keycode_s=47;
- keycode_t=48;
- keycode_u=49;
- keycode_v=50;
- Keycode_w=51;
- Keycode_x=52;
- Keycode_y=53;
- keycode_z=54;
- Keycode_comma=55;
- Keycode_period=56;
- keycode_alt_left=57;
- Keycode_alt_right=58;
- keycode_shift_left=59;
- keycode_shift_right=60;
- keycode_tab=61;
- Keycode_space=62;
- keycode_sym=63;
- Keycode_explorer=64;
- Keycode_envelope=65;
- Keycode_enter=66;
- Keycode_del=67;
- Keycode_grave=68;
- keycode_minus=69;
- keycode_equals=70;
- keycode_left_bracket=71;
- keycode_right_bracket=72;
- Keycode_backslash=73;
- keycode_semicolon=74;
- Keycode_apostrophe=75;
- Keycode_slash=76;
- Keycode_at=77;
- keycode_num=78;
- keycode_headsethook=79;
- keycode_focus=80;//*camera*focus
- keycode_plus=81;
- keycode_menu=82;
- keycode_notification=83;
- Keycode_search=84;
- keycode_media_play_pause=85;
- keycode_media_stop=86;
- keycode_media_next=87;
- keycode_media_previous=88;
- keycode_media_rewind=89;
- Keycode_media_fast_forward=90;
- Keycode_mute=91;
Android uses root permissions to enable background simulation of global keys, touch-screen event methods (similar to Key wizard)