根据android developer documentation
Android 8.0(API级别26)引入了几个相关的新权限 电话:ANSWER_PHONE_CALLS权限允许您的应用 以编程方式接听来电。处理传入 在你的应用程序中打电话,你可以使用acceptRingingCall()方法。
我的应用如何为来电提供语音解答?
但我没有找到任何关于此的指南或示例。 所以
我想用语音自动回答(来自app中的原始文件夹) (我需要指南或示例代码,如答录机)
答案 0 :(得分:1)
https://developer.android.com/reference/android/telecom/TelecomManager.html#acceptRingingCall()
接受应答呼叫未弃用API LEVEL Q
尝试{ Toast.makeText(CallAnswerDialog.this,“ btn_answer click”,Toast.LENGTH_SHORT).show();
TelecomManager tm = (TelecomManager) CallAnswerDialog.this.getSystemService(Context.TELECOM_SERVICE);
if (tm == null) {
// whether you want to handle this is up to you really
throw new NullPointerException("tm == null");
}
tm.acceptRingingCall(); // is deprecated Now API Level Q
btn_answer.setVisibility(View.GONE);
}catch (Exception e){
e.printStackTrace(); }
答案 1 :(得分:0)
要接听电话,只能在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上运行...:(
答案 2 :(得分:0)
在来电时,我想接收它。然后,从我的应用程序,在原始目录的通话过程中自动播放音频文件(例如“ hi,请留下message.mp3”),并且对方应听到声音。在Android O中有可能吗? 如果可能的话,为什么我在您的答案中看不到声音消息的原始目录?
答案 3 :(得分:0)
来自Google的更新:
telecomManager.acceptRingingCall();
telecomManager.acceptRingingCall(false);
telecomManager.endCall();
这三个命令在Android Q和Android Q上均已弃用