Reputation: 244
My application needs to answer incoming calls programmatically (it's an enterprise telephony app that allows dialing via office). Up until Android 4.4, I was using the Headset Hook method
Unfortunately, in 5.0 and 6.0 that doesn't seem to work anymore. I've tried experimenting with KeyEvent.ACTION_DOWN and ACTION_UP, without and without the android.permission.CALL_PRIVILEGED permission. I also tried swapping the KeyEvent.KeyCODE_HEADSETHOOK for KeyEvent.KEYCODE_CALL to no avail, as well as triggering the headset plug. All that no nothing moves on my Nexus 6P. Sending "input keyevent 79" does not do anything either.
Being an enterprise telephony app, it needs to run on a variety of unrooted devices, so calling service is out, as well as anything that requires android.permission.MODIFY_PHONE_STATE.
I know this is possible because I have another telephony app on my phone that does just that without requiring any special permissions.
Upvotes: 1
Views: 1300
Reputation: 244
I managed to reverse engineer an application where it worked. Here's how to get it done..
Note that the code is for Xamarin but I'm sure you can bring it back to Java Format ;)
Instrumentation inst = new Instrumentation();
inst.SendKeySync(new KeyEvent(KeyEventActions.Down, Keycode.Headsethook));
inst.SendKeySync(new KeyEvent(KeyEventActions.Up, Keycode.Headsethook));
Hope this will help somebody save some time..
Upvotes: 1