问题描述
在 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
12-09 18:11:25.195 16833-16833/li.doerf.leavemealoneE/TelephonyServiceCallHangup:缺少权限 MODIFY_PHONE_STATE,不能挂断电话
由于 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 上以编程方式结束来电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!