I am trying to programmatically control incoming call (Accept and reject) Target Android 6.0 and above in my companion app.
Working method but deprecated
telecomManager.acceptCall() and telecomManager.endCall() This method is working fine till Android 10 and also in virtual Android 11 but in developers site it says it is deprecated.
This method was deprecated in API level 29.
Companion apps for wearable devices should use the InCallService API instead.
Partially working method
By simulation headset press button key event found that the call can be controlled. The following is my implementation
void sendHeadsetHookLollipop() {
MediaSessionManager mediaSessionManager = (MediaSessionManager) getApplicationContext().getSystemService(Context.MEDIA_SESSION_SERVICE);
try {
List<MediaController> mediaControllerList = mediaSessionManager.getActiveSessions
(new ComponentName(getApplicationContext(), NotificationReceiverService.class));
for (MediaController m : mediaControllerList) {
if ("com.android.server.telecom".equals(m.getPackageName())) {
m.dispatchMediaButtonEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
log.info("HEADSETHOOK sent to telecom server");
break;
}
}
} catch (SecurityException e) {
log.error("Permission error. Access to notification not granted to the app.");
}
}
in the above piece of code, I am able to answer the ringing call. To reject, I need to simulate a long press of the same KeyEvent.