我试图在 Android Oreo 中以编程方式终止一个电话。
这是我的源代码。
public boolean killCall(Context context) {
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Method m1 = tm.getClass().getDeclaredMethod("getITelephony");
m1.setAccessible(true);
Object iTelephony = m1.invoke(tm);
Method m2 = iTelephony.getClass().getDeclaredMethod("silenceRinger");
Method m3 = iTelephony.getClass().getDeclaredMethod("endCall");
m2.invoke(iTelephony);
m3.invoke(iTelephony);
} catch (Exception ex) { // Many things can go wrong with reflection calls
Log.d(TAG,"PhoneStateReceiver **" + ex.getCause());
ex.getCause().printStackTrace();
return false;
}
return true;
}
但我得到例外java.lang.SecurityException: Neither user 10187 nor current process has android.permission.CALL_PHONE.
虽然我已经在清单中定义了权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
我还使用弹出窗口获取运行时权限,当我查看应用程序权限时,电话权限也在那里启用。我想知道为什么会发生这种情况,而且令人惊讶的是,这种情况仅发生在 Android Oreo 上,而不是在较低版本上。任何有用的建议都会很棒。