在Android 7.1之前,可以通过使用ITelephony.endCall()方法结束传入呼叫,并为您的应用提供权限android.permission.CALL_PHONE和android.permission.READ_PHONE_STATE.
在Android 8.0 Oreo(API 26)上执行相同操作时,我收到此错误
12-09 18:11:25.195 16833-16833/li.doerf.leavemealone
E/TelephonyServiceCallHangup: Missing permission MODIFY_PHONE_STATE,
cannot hangup call
由于MODIFY_PHONE_STATE是受保护的权限,我的应用无法获取.有没有办法以编程方式结束Android 8.0上的来电?
解决方法:
将App Target和Compile级别更改为28.
并遵循权限.
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
在MyPhoneStateListener类的onCallStateChanged方法上添加以下代码.
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();
}
}
}
}