Hi,

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 manage 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.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to