以编程方式断开调用不能在android 8.0(Oreo)中工作的调用

时间:2017-12-15 13:40:58

标签: android android-permissions telephonymanager android-phone-call

在Android 7.1之前,可以使用ITelephony.endCall()方法结束来电,并为您的应用提供权限android.permission.CALL_PHONE和android.permission.READ_PHONE_STATE。

在Android 8.0 Oreo(API 26)上执行相同操作时,我收到此错误

  

12-14 17:37:26.160 20962-20962 / com.xinthe.carmode W / System.err:   java.lang.reflect.InvocationTargetException 12-14 17:37:26.161   20962-20962 / com.xinthe.carmode W / System.err:at   java.lang.reflect.Method.invoke(Native Method)12-14 17:37:26.161   20962-20962 / com.xinthe.carmode W / System.err:at   com.xinthe.carmode.listeners.MyPhoneStateListener.disconnectCall(MyPhoneStateListener.java:108)   12-14 17:37:26.161 20962-20962 / com.xinthe.carmode W / System.err:at   com.xinthe.carmode.listeners.MyPhoneStateListener.onCallStateChanged(MyPhoneStateListener.java:51)   12-14 17:37:26.161 20962-20962 / com.xinthe.carmode W / System.err:at   android.telephony.PhoneStateListener $ 1.handleMessage(PhoneStateListener.java:338)   12-14 17:37:26.161 20962-20962 / com.xinthe.carmode W / System.err:at   android.os.Handler.dispatchMessage(Handler.java:105)12-14   17:37:26.161 20962-20962 / com.xinthe.carmode W / System.err:at   android.os.Looper.loop(Looper.java:164)12-14 17:37:26.161   20962-20962 / com.xinthe.carmode W / System.err:at   android.app.ActivityThread.main(ActivityThread.java:6809)12-14   17:37:26.161 20962-20962 / com.xinthe.carmode W / System.err:at   java.lang.reflect.Method.invoke(Native Method)12-14 17:37:26.161   20962-20962 / com.xinthe.carmode W / System.err:at   com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)   12-14 17:37:26.161 20962-20962 / com.xinthe.carmode W / System.err:at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)12-14   17:37:26.162 20962-20962 / com.xinthe.carmode W / System.err:引起:   java.lang.SecurityException:既不是用户10304也不是当前进程   有android.permission.CALL_PHONE。 12-14 17:37:26.162   20962-20962 / com.xinthe.carmode W / System.err:at   android.os.Parcel.readException(Parcel.java:1942)12-14 17:37:26.162   20962-20962 / com.xinthe.carmode W / System.err:at   android.os.Parcel.readException(Parcel.java:1888)12-14 17:37:26.162   20962-20962 / com.xinthe.carmode W / System.err:at   com.android.internal.telephony.ITelephony $存根$ Proxy.endCall(ITelephony.java:1955)   12-14 17:37:26.162 20962-20962 / com.xinthe.carmode W / System.err:...   10更多12-14 17:37:26.162 20962-20962 / com.xinthe.carmode   E /结束呼叫错误:致命错误:无法连接到电话   子系统12-14 17:37:26.162 20962-20962 / com.xinthe.carmode   E /结束调用错误:异常对象:   java.lang.reflect.InvocationTargetException

以下是READ_PHONE_STATE运行时权限的代码。

String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.ANSWER_PHONE_CALLS};
void permissionCheck() {

    if (!hasPermissions(this, PERMISSIONS)) {
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }
}

public static boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:3)

看起来您忘了为CALL_PHONE添加运行时权限 引起:java.lang.SecurityException:用户10304和当前进程都没有android.permission.CALL_PHONE。

答案 1 :(得分:0)

One possible solution is via Android Accessibility Service. You can find the resource id of Cancel call button and Click that via 11y Service!

Check out this link which uses Android Accessibility Service to press "Force Stop" button just like Greenify App.

How to click button in settings using AccessibilityService?

Hope this helps!