问题描述
在Android 7.1之前的版本中,可以使用 ITelephony.endCall()
方法并为您的应用授予 android.permission.CALL_PHONE
和 android.permission.READ_PHONE_STATE
.
Up to Android 7.1 it was possible to end an incoming call by using the ITelephony.endCall()
method and giving your app the permissions android.permission.CALL_PHONE
and android.permission.READ_PHONE_STATE
.
在Android 8.0 Oreo (API 26)
上执行相同操作时,出现此错误
When doing the same on Android 8.0 Oreo (API 26)
, i get this error
由于 MODIFY_PHONE_STATE
是受保护的权限,因此我的应用无法获取它.有没有办法以编程方式结束Android 8.0+
上的传入呼叫?
Since MODIFY_PHONE_STATE
is a protected permission, my app cannot get it. Is there a way to programmatically end an incoming call on Android 8.0+
?
推荐答案
将应用程序目标和编译级别更改为28.
Changed App Target and Compile level to 28.
并具有权限.
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
在MyPhoneStateListener类的onCallStateChanged方法上添加以下代码.
Add the following code on onCallStateChanged method of MyPhoneStateListener class.
public void endCall() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
TelecomManager tm = (TelecomManager) mcontext.getSystemService(Context.TELECOM_SERVICE);
if (tm != null) {
boolean success = tm.endCall();
}
// success == true if call was terminated.
} else {
if (mcontext != null) {
TelephonyManager telephony = (TelephonyManager) mcontext
.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
// telephonyService.silenceRinger();
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
这篇关于如何在Android 8.0 Oreo上以编程方式结束来电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!