简体   繁体   English

订购广播无法接受来电

[英]Ordered broadcast for accepting incoming call not working

Im making an app that creates custom call receive screens. 我正在制作一个创建自定义呼叫接收屏幕的应用程序。 I've managed to show the screens and add a button for rejecting incoming call. 我设法显示了屏幕并添加了一个拒绝来电的按钮。 For accepting incoming calls, my code isn't working. 为了接受来电,我的代码无法正常工作。 I'm trying to use ordered broadcasts for accepting the call but the code isn't working. 我正在尝试使用有序广播来接听电话,但是代码不起作用。 Its not throwing any exceptions, or crashing. 它不会引发任何异常或崩溃。 It just doesn't do anything. 它什么也没做。 Here is the code I'm using: 这是我正在使用的代码:

Intent answer = new Intent(Intent.ACTION_MEDIA_BUTTON);
                answer.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
                sendOrderedBroadcast(answer, null);

I'm revising the old question. 我正在修改旧问题。 Thanks. 谢谢。

As the Exception message says, you lack the permission to answer phone calls. 就像“异常”消息中所说,您没有应答电话的权限。

Android uses a permissions system to guard an app's access to sensitive functionality. Android使用权限系统来保护应用程序对敏感功能的访问。 When you want to use certain functionality, you need to declare the permission to do that in your manifest. 当您要使用某些功能时,需要在清单中声明执行此操作的权限。

There are two kinds of permissions: normal and dangerous . 有两种权限: 普通危险 Normal ones are granted automatically if they are declared in the manifest. 如果在清单中声明了普通选项,则会自动授予它们。 Dangerous ones additionally need runtime approval: Your app requests the declared permission at runtime and Android prompts the user for their approval. 危险用户还需要运行时批准:您的应用在运行时请求声明的权限,而Android会提示用户进行批准。 Read more about this here . 在此处阅读有关此内容的更多信息。

The permission you are looking for is Manifest.permission.ANSWER_PHONE_CALLS . 您正在寻找的权限是Manifest.permission.ANSWER_PHONE_CALLS

Declare it in your manifest like this: 像这样在清单中声明它:

<manifest […]>

    <uses-permission android:name="android.permission.ANSWER_PHONE_CALLS"/>
    <!-- declare other permissions the same way here -->

    […]

</manifest>

At runtime, check whether you have the required permission and only perform the action if you do: 在运行时,检查您是否具有所需的权限,并且仅在以下情况下才执行操作:

if(ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ANSWER_PHONE_CALLS)
        == PackageManager.PERMISSION_GRANTED) {
    // Your code using the dangerous permission goes here
} else {
    // request the permission, see below
}

Where thisActivity is your current Activity. 其中thisActivity是您当前的活动。

To request the permission, use: 要请求许可,请使用:

ActivityCompat.requestPermissions(thisActivity,
            new String[] {Manifest.permission.ANSWER_PHONE_CALLS},
            PERMISSIONS_ANSWER_PHONE_CALLS);

Where PERMISSIONS_ANSWER_PHONE_CALLS is an int constant you defined with a unique value. 其中PERMISSIONS_ANSWER_PHONE_CALLS是您使用唯一值定义的int常量。

The permission is requested asynchronously, meaning you will be notified when the permission is granted or denied. 权限是异步请求的,这意味着在授予或拒绝权限时会通知您。 To receive this notification, override this callback: 要接收此通知,请覆盖此回调:

@Override
public void onRequestPermissionsResult(int requestCode,
        String[] permissions, int[] grantResults) {
    switch(requestCode) { // check which permission was requested
        case PERMISSIONS_REQUEST_ANSWER_PHONE_CALLS:
            // check whether permission was granted
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission was granted; you can call your method requiring this permission now
            } else {
                // permission was denied, you probably want to show a message that your feature won't be available
            }
            return;
            // code for other permission requests goes here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM