Android uses root permissions to enable background simulation of global keys, touch-screen event methods (similar to key wizard)
Sometimes we need to use Android to simulate the system keys in the background, such as adjusting the volume (simulating the volume key), closing the app that is running in the foreground (simulating the back button), or simulating a touch-screen event. However, for the native Android system, the background process to close the foreground process, or even simulate user events, and then manipulate the entire system, is not in line with the system security principle, if there is a virus or malicious software exploit, it can be very dangerous.
For some special reasons, I happen to need to implement such a feature without the ability to compile my own Android system (but can take advantage of root permissions, because root access is relatively simple to obtain, and many users of Android devices have root). Online also see a lot of people are mentioning similar problems, many people discussed for half a day, the results are no solution. So I took a lot of energy and finally found the solution.
On the internet to find a lot of information, mainly found two ways: instrumentation and Iwindowmanager.
Use Instrumentation interface: For non-self-compiled Android system, can not get the system signature, only in front of the simulation button, not background simulation.
One is to useinstrumentation interface, this interface was originally used to test the software and left out. After trying, it was found that the interface can simulate keystrokes, but only if the app is in the foreground. While the application is in the foreground, the analog button is basically not much of a function (the analog key operation application itself does not seem very significant).
when the app is in the background, thisInstrumentationthe interface becomes invalid. The explanation on the web is that using this interface in the background requires system permissions, which are added in manifestandroid:shareduserid= "Android.uid.system". And what is the problem? The APK that declares the system permission can be installed on the Android device only if it has the system signature, for example, the system comes with the phone, SMS, essentially the APK program, but these apps have system privileges.
The Android system has a signature mechanism, and the APK can only be installed if it has a digital signature. Typically, when you debug, the default eclipse automatically signs it, using the debug signature. When the app is published, the developer signs the APK with its own unique digital signature file (this file can be generated with eclipse and the signature can be done by Eclipse). APK has a new version of the installation, if the signature is detected inconsistent, the system will prompt the signature is inconsistent, only uninstall the old version to install. This mechanism has avoided, to some extent, the modification of the official apk by the third party, or even the illegal implantation of the virus (of course, if the user actively uninstall the old version of the official app, install the new version of the unofficial apk is also possible). and different apps with the same signature can share some data between them.
and how does the system signature get it? When compiling the Android system, a system-signed digital signature file is compiled together. For a system that has already been compiled, or to fit a different system, it is not possible to obtain the digital signature file, so the APK can not be signed system. Finally, the Uid.system property of the APK can not be installed, so the instrumentation interface Background Simulation button method, only in the case of self-compiled system can be used.
invoking using the Reflection methodsystem Iwindowmanager Hidden API: Poor compatibility, poor stability, error prone. In addition, the actual compilation error occurred, the reason is temporarily unknown.
There is another way online. There are some hidden APIs in the Android system, often using Java's permission restrictions, which make these APIs impossible to invoke. But by reflection, you can break through Java's permission limits. The API that simulates keystrokes and touch-screen events is hidden in Iwindowmanager. Try the online method, download to a jar file compiled by Android source code, add it to the project, and then useLaunchsome code was written to try to invoke the hidden API. As a result, eclipse does not respond directly to the compilation, possibly because the computer is not configured enough and the jar file is too large. Tried several times without success, and considering that this method has a lot of drawbacks, and it is likely to eventually need system permissions (many of the online articles are not very clear), and then gave up this method.
Android Analog key issues summary [using Iwindowmanager.injectkeyevent method]
http://blog.csdn.net/xudongdong99/article/details/8857173
Using hidden APIs in Android (lots of illustrations)
http://163liufuliang.blog.163.com/blog/static/331651862013119114431760/
JNI calls the C Program simulation button: Still a permissions issue.
Referring to some of the online information on the possible ideas, found that the rest can be thought of the method is implemented with JNI, by calling C + + program to simulate keys. Not familiar with the Linux underlying programming, the online reference to some code, in Ubuntu under the writing of a key simulation program, a smooth compilation run through. Then began to learn the Jni method of compiling, first in the C program layer wrote a simple addition operation, compile and run the test pass, and then put the code of the analog button to paste in. Look forward to writing the Android Java layer Code, compiling, downloading, executing the program, but found completely ineffective.
To see exactly which step was wrong,Changed in the C program,use Logcat to print out the return value of the C program, found that when the key device is turned on error, it seems that the issue of authority is certainly.
Although the system has been root,apk also allows the use of root permissions, but root permissions can not be passed to the C program, insufficient permissions, the program cannot execute. On the Internet to find a information about Linux, Android permissions, also did not find out what train of thought. in fact, at that time very confused, in the Linux system, the root authority is the highest permissions, Android is no exception, there are articles, root privileges > System permissions > User rights. Although it is possible to get root permissions and not complete the tasks that can be done by system permissions, the total feeling should not be.
Android Key Wizard: Using root privileges without system signature, it is possible to implement background simulation keys and touch screen events.
It was hopeless, and it was estimated that only self-compiled systems could solve the problem.at that time, suddenly remembered the key wizard software. Used in the previous version of the computer, in the Android Market to find, sure enough also has an Android version. Download use found, the key Wizard can be implemented in the background to simulate the key operation, requires root authority, but what the principle is unknown. I want to try to decompile the source view, but there were some problems, the anti-compilation did not succeed. The principle of searching the Android Key wizard on the Internet, in addition to the previous two source-dependent environment to use the API, also did not find the results. But at least that means using root permissionsWithout the need for a system signature,it is possible to implement analog keys and be compatible with a large number of Android devices.
Final Solution: Use the shell to invoke the ADB instruction implementation.
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 the problem is that the reaction rate is very slow,in particular, 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.
/** * 用root权限执行Linux下的Shell指令 * * @author jzj * @since 2014-09-09 */public class RootShellCmd {private OutputStream os;/** * 执行shell指令 * * @param cmd * 指令< Span class= "com" style= "Color:rgb (147,161,161)" > */ < Span class= "PLN" style= "Color:rgb (72,72,76)" " public final void exec ( string Span class= "PLN" style= "Color:rgb (72,72,76)" > cmd ) { < Span class= "PLN" style= "Color:rgb (72,72,76)" > try Span class= "PLN" style= "Color:rgb (72,72,76)" " { < Span class= "PLN" style= "Color:rgb (72,72,76)" > if Span class= "PLN" style= "Color:rgb (72,72,76)" " ( Span class= "PLN" style= "Color:rgb (72,72,76)" >os == null ) Span class= "pun" style= "Color:rgb (147,161,161)" >{ < Span class= "PLN" style= "Color:rgb (72,72,76)" >os = " Span class= "PLN" style= "Color:rgb (72,72,76)" " runtime getruntime (). exec ( "su" getoutputstream (); }os.write(cmd.getBytes());< Span class= "PLN" style= "Color:rgb (72,72,76)" >os flush (); < Span class= "PLN" style= "Color:rgb (72,72,76)" " } Span class= "PLN" style= "Color:rgb (72,72,76)" > catch Span class= "PLN" style= "Color:rgb (72,72,76)" " ( Span class= "Typ" style= "Color:teal" >exception e { e.printStackTrace();}}/** * 后台模拟全局按键 * * @param keyCode * 键值 */< Span class= "PLN" style= "Color:rgb (72,72,76)" " public final void Simulatekey ( int KeyCode ) Span class= "pun" style= "Color:rgb (147,161,161)" >{ exec("input keyevent " + keyCode + "\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 have pointed out the disadvantages: slower response times. As mentioned earlier, I have also been given a solution to the same problem.
This article by Purplesword (jzj1993) Original, reproduced please specify the original URL http://blog.csdn.net/jzj1993
Android uses root permissions to enable background simulation of global keys, touch-screen event methods (similar to Key wizard)