Android solves the problem of sending keyevent to different processes
Recently, the function of the remote Controller, the function of the phone as a TV remote control to deal with. On the handset's client sends the message to the TV's Android service side, the service side receives the client's request message, simulates the KeyEvent command, sends the key value.
The simplest send command is the following code:
public static void Simulatekeystroke (final int keycode) {
new Thread (new Runnable () {public
void run () {
//T Odo auto-generated Method Stub
try {
Instrumentation inst=new instrumentation ();
Inst.sendkeydownupsync (keycode);
} catch (Exception e) {
//Todo:handle Exception
}}}
). Start ();
This approach is not problematic in the current interface and in the same process, and can be realized by the basic requirements. But when I open the service, press home to exit the service or interface to the background, and then send a message to the Server service server to simulate the key, the unfortunate thing is sent:
Injecting to another application requires inject_events permission
Prompt does not inject_events this permission. In addition, in the Androidmanifest.xml file to add this permission, and then run, the problem is not resolved, because the above code ultimately or call the Windowsmanagerservice inside the Injectkeyevent method, This method verifies the PID and UID of your current program, and if the two returns-1 when the key is distributed-prompts the error above.
Well, there's a whole lot of crap, and here's the way to really solve the problem.
All kinds of Google Baidu online, can not find the answer you need.
One way to think about it is (not yet validated):
Through the JNI method will kernel send the KeyEvent method to use NDK to encapsulate the method, makes the library to the Java call, thus bypasses the Android Windowsmanagerservice verification, this is the solution idea which I thought initially, but has not verified.
Another way to pass validation is:
Change the userid of your service to the system level and add the following code to the manifest:
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
package= " Com.xuzhitech.remote.server "
android:versioncode=" 1 "
android:versionname=" 1.0 "
android: Shareduserid= "Android.uid.system" >
Plus this code, you need to compile in the code to be effective, add Android.mk file:
Local_path: = $ (call My-dir)
include $ (clear_vars)
local_module_tags: = Optional
Local_src_files: = $ (call All-subdir-java-files)
local_package_name: = Remoteandroidserver
local_certificate: = Platform
#LOCAL_ Certificate: = Share
local_overrides_packages: = Home
include $ (build_package)
The local_certificate here will be compiled using platform instead of share.
When you get here, you can simulate the keys across the process.
Thank you for reading, I hope to help you, thank you for your support for this site!