my goal is to target with a keyevent a specific application running either in foreground or background from a background service.
I tried many solutions, but have not yet managed to do it.
The few solutions tried (all from a background running service):
With a broadcast, I tried to target the first application (for example the phone app) that would manage the key event
KeyEvent lKey1Up = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_ENDCALL);
KeyEvent lKey1Dwn = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_ENDCALL);
Intent lKey1UpIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
Intent lKey1DwnIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
lKey1UpIntent.putExtra(Intent.EXTRA_KEY_EVENT, lKey1Up);
lKey1DwnIntent.putExtra(Intent.EXTRA_KEY_EVENT, lKey1Dwn );
sendOrderedBroadcast(lKey1UpIntent, null);
sendOrderedBroadcast(lKey1DwnIntent, null);
=> Nothing happens with my foreground phone app when the broadcast is performed while I am in a phone call state (OFFHOOK). Indeed, I was nearly sure this would not work since I have no way to specificely target the phone app.
With Instrumentation, I tried to target the application that has the focus :
Instrumentation lInst = new Instrumentation();
KeyEvent lKey1Up = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_1);
KeyEvent lKey1Dwn = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_1);
lInst.sendKeySync(lKey1Up);
lInst.sendKeySync(lKey1Dwn);
also tried with a single call to :
lInst.sendKeyDownUpSync(KeyEvent.KEYCODE_1)
=> Application crashes (also during phone call) looks like I cannot use Instrumentation out of a testing purpose
Eventually, I thought about using
superDispatchKeyEvent(KeyEvent)
but since I don't know how to target a specific window from the targeted running application (and I have none in my service, indeed), I don't know how to use it at all.
And before anyone asks, I added the
android.permission.INJECT_EVENTS
android.permission.MODIY_PHONE_STATE
in my manifest in order to be sure all I do is "allowed".
Then... thanks first for reading until here, and now :
- some of you know how I can manag开发者_开发百科e to do target a specific application with a keystroke event from a service?
- some of you know how to do the same with the phone application specificely?
Thanks in advance for your help.
my goal is to target with a keyevent a specific application running either in foreground or background from a background service.
This is not possible, because it is a security hole. Allowing application A to inject key events into application B raises all sorts of ugly malware possibilities.
You can use something like this, but need rooted device:
public static void inputKeyEvent(String keyCodeString) {
try {
int keyCode = Integer.parseInt(keyCodeString);
try {
Instrumentation m_Instrumentation = new Instrumentation();
m_Instrumentation.sendKeyDownUpSync(keyCode);
} catch (SecurityException e) {
try {
Process processKeyEvent = Runtime.getRuntime().exec("/system/xbin/su");
DataOutputStream os = new DataOutputStream(processKeyEvent.getOutputStream());
os.writeBytes("input keyevent " + keyCode + "\n");
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
精彩评论