Для ответа на телефонный звонок это доступно только с Android O. Раньше это могло не работать.Вот код для этого:
/**returns true iff we are sure that answering the phone call worked*/
fun answerCall(context: Context): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(context, Manifest.permission.MODIFY_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
telecomManager.acceptRingingCall()
return true
}
return false
}
// https://stackoverflow.com/a/29651130/878126
// for HTC devices we need to broadcast a connected headset
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
val broadcastConnected = "htc" == Build.MANUFACTURER.toLowerCase(Locale.US) && !audioManager.isWiredHeadsetOn
if (broadcastConnected)
broadcastHeadsetConnected(context, false)
try {
try {
Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK))
} catch (e: IOException) {
// Runtime.exec(String) had an I/O problem, try to fall back
val enforcedPerm = "android.permission.CALL_PRIVILEGED"
val btnDown = Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK))
val btnUp = Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK))
context.sendOrderedBroadcast(btnDown, enforcedPerm)
context.sendOrderedBroadcast(btnUp, enforcedPerm)
}
} finally {
if (broadcastConnected)
broadcastHeadsetConnected(context, false)
}
return false
}
Для того, чтобы повесить трубку, это было официально добавлено на Android P, но обходной путь, похоже, работает в большинстве случаев, которые я до сих пор пробовал (не работает наNexus 5x с Android 8.1, например).Вот код для этого:
@SuppressLint("PrivateApi")
fun endCall(context: Context): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
if (telecomManager != null && ContextCompat.checkSelfPermission(context, Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED) {
telecomManager.endCall()
return true
}
return false
}
//use unofficial API for older Android versions, as written here: https://stackoverflow.com/a/8380418/878126
try {
val telephonyClass = Class.forName("com.android.internal.telephony.ITelephony")
val telephonyStubClass = telephonyClass.classes[0]
val serviceManagerClass = Class.forName("android.os.ServiceManager")
val serviceManagerNativeClass = Class.forName("android.os.ServiceManagerNative")
val getService = serviceManagerClass.getMethod("getService", String::class.java)
val tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder::class.java)
val tmpBinder = Binder()
tmpBinder.attachInterface(null, "fake")
val serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder)
val retbinder = getService.invoke(serviceManagerObject, "phone") as IBinder
val serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder::class.java)
val telephonyObject = serviceMethod.invoke(null, retbinder)
val telephonyEndCall = telephonyClass.getMethod("endCall")
telephonyEndCall.invoke(telephonyObject)
return false
} catch (e: Exception) {
e.printStackTrace()
return false
}
}
Итак, для ответа и отклонения вызовов самый безопасный способ сделать это - запустить его на Android P ...: (