Detailed explanation of Android cross process simulation key (keyevent) example

Android solves the problem that different processes send keyevents

Recently, I have been working on the function of remote controller, which makes the mobile phone into a TV remote control. Send a message from the client of the mobile phone to the Android server of TV. The server receives the request message from the client, simulates the keyevent command and sends the key value.

The simplest sending command is the following code:

This method has no problem in the current interface and the same process, and can meet the basic requirements. However, when I still turn on the service, press home to exit the service or interface to the background, and then send a message to the server service through the client to simulate the key press, the unfortunate thing is sent:

Injecting to another application requires INJECT_ EVENTS permission

Prompt no inject_ Events this permission. If not, add the permission in the androidmanifest.xml file and run it again. The problem is still not solved because the above code finally calls the injectkeyevent method in windowsmanagerservice, which will verify the PID and uid of your current program. If both return - 1 when distributing the key key, you will be prompted with the above error

Well, there's a lot of nonsense. Here's the real way to solve this problem.

All kinds of Google and Baidu on the Internet can't find the answer they need.

One method is (not yet verified):

Through JNI's method, the method of sending keyevent from the kernel is encapsulated into a method with NDK and made into a library for Java calls, so as to bypass the verification of Android windowsmanagerservice. This is the solution I thought of at the beginning, but it has not been verified.

Another verified method is:

Change the userid of your service to system level, and add the following code in the manifest:

This code needs to be compiled in the source code to take effect. Add the android.mk file:

Local here_ Certificate is compiled using platform, not share.

Here, you can simulate buttons across processes.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>