如何在Android 8.0 Oreo上以编程方式结束来电

问题描述 投票:14回答:4

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

当在Android 8.0 Oreo (API 26)上做同样的事情时,我得到了这个错误

12-09 18:11:25.195 16833-16833 / li.doerf.leavemealone E / TelephonyServiceCallHangup:缺少权限MODIFY_PHONE_STATE,无法挂断电话

由于MODIFY_PHONE_STATE是受保护的权限,我的应用程序无法获得它。有没有办法以编程方式结束Android 8.0+上的来电?

android android-permissions telephony android-8.0-oreo
4个回答
1
投票

请查看以下2链接,这对您有所帮助:

Permission is only granted to system app, in Manifest

https://source.android.com/devices/tech/config/perms-whitelist

上面的链接用于白名单你的MODIFY_PHONE_STATE权限。这是出于安全目的,因此,如果您想在此时访问此类权限,请在致电您的操作后将您的权限列入白名单。

我认为根据android oreo最新更新,您可以使用内置方法实现接收您的活动中的电话,但这些不提供开发人员来处理呼叫结束。获取(读取)接听电话的电话号码,但您需要定义它的许可。我认为我的上述描述足以让人理解

希望你理解并关闭这个问题。


0
投票

尝试使用qazxsw poi提供的答案中的反射。

它适用于READ_PHONE_STATE和CALL_PHONE权限。根据here的说法,这些权限可用于非系统应用程序


0
投票

您需要在清单中添加此权限:

android docs

0
投票

我找到了一个解决方案。它需要注册为NotificationListenerService,然后解析操作并发送正确的PendingIntents:

<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>

然后在AndroidManifest.xml中声明以下内容:

public class NotificationListener extends NotificationListenerService {
    private static final String TAG = "NotificationListener";
    private PendingIntent answerIntent;
    private PendingIntent hangupIntent;

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        String packageName = sbn.getPackageName();
        if ("com.google.android.dialer".equals(packageName)) {
            Notification notification = sbn.getNotification();
            for (Notification.Action action : notification.actions) {
                String title = String.valueOf(action.title);
                if ("hang up".equalsIgnoreCase(title) || "decline".equalsIgnoreCase(title)) {
                    hangupIntent = action.actionIntent;
                } else if ("answer".equalsIgnoreCase(title)) {
                    answerIntent = action.actionIntent;
                }
            }
        }
    }

    @Override
    public void onListenerConnected() {
        Log.i(TAG, "Listener connected");
        for (StatusBarNotification sbn : getActiveNotifications()) {
            onNotificationPosted(sbn);
        }
    }

    public void answerCall() {
        try {
            answerIntent.send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }

    public void endCall() {
        try {
            hangupIntent.send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }
}

还有一个步骤:用户必须手动启用您的应用程序作为通知侦听器。您可以使用以下代码显示包含说明的对话框,然后将用户发送到设置页面:

    <service
        android:name=".NotificationListener"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>
© www.soinside.com 2019 - 2024. All rights reserved.