我正试图在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.
虽然我已在manifest
中定义了权限<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上,而不是在较低版本上。任何有用的建议都会很棒。