ITelephony 接口方法 answerRingingCall() 不工作

ITelephony interface method answerRingingCall() not working

我想从我的应用程序 receive Incoming call,我正在使用 ITelephony.aidlEnd and receive call。我已经编写了以下代码来在按下按钮时接听电话,大多数情况下我能够在所有设备上结束通话但是当我尝试接听电话时它不起作用。

此(呼叫应答)代码在我测试过的某些设备上运行良好(摩托罗拉** e3 6.0 android 安全补丁 2016 年 6 月 1 日,honor 4.4.4,YU 4.4.4. 等)it's not working on(Xiaomi note 4 6.0.1, lyf 6.O.1 android 安全补丁 2017 年 3 月 1 日)` 可能是由于 android 安全补丁更新引起的在最新版本上。

我认为 google 出于 security 的原因,在 android security patch 1 Jun 2016 to 1 March 2017.[的最新更新中仅允许 system apps 的一些 permission

请帮忙,谢谢

    private void m13352k() {
     try {
        TelephonyManager telephonyManager = (TelephonyManager) 
        getSystemService("phone");

        Method declaredMethod = 
        Class.forName(telephonyManager.getClass().getName()).
        getDeclaredMethod("getITelephony", new Class[0]);
        declaredMethod.setAccessible(true);
        ITelephony iTelephony = (ITelephony) 
        declaredMethod.invoke(telephonyManager, new Object[0]);
        iTelephony.silenceRinger();
        iTelephony.answerRingingCall();
    } catch (Exception e) {
        try {
            m13350j();
        } catch (Exception e2) {
            m13349i();
        }
    }
}

    private void m13349i() {
    try {
        Intent intent = new Intent(callerIdConfig.mediaButton());
        intent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0, 79));
        sendOrderedBroadcast(intent, callerIdConfig.callPrivileged());
        intent = new Intent(callerIdConfig.mediaButton());

        intent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(1, 79));
        sendOrderedBroadcast(intent, callerIdConfig.intentCallPrivileged());
        intent = new Intent(callerIdConfig.headsetPlug());
        intent.addFlags(1073741824);

        intent.putExtra("state", callerIdConfig.m13633f());
        intent.putExtra("name", callerIdConfig.headset());
        sendOrderedBroadcast(intent, null);
    } catch (Exception e) {
        e.printStackTrace();
    }}

    private void m13350j() {
    Intent intent = new Intent("android.intent.action.HEADSET_PLUG");
    intent.addFlags(1073741824);
    intent.putExtra("state", callerIdConfig.m13633f());//0
    intent.putExtra("name", callerIdConfig.headset());
    sendOrderedBroadcast(intent, null);

    intent = new Intent(callerIdConfig.mediaButton());
    intent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0, 79));
    sendOrderedBroadcast(intent, callerIdConfig.intentCallPrivileged());
    intent = new Intent(callerIdConfig.mediaButton());

    intent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(1, 79));
    sendOrderedBroadcast(intent, callerIdConfig.intentCallPrivileged());
    intent = new Intent("android.intent.action.HEADSET_PLUG");
    intent.addFlags(1073741824);

    intent.putExtra("state", callerIdConfig.m13629e());//1
    intent.putExtra("name", callerIdConfig.headset());//"Headset"
    sendOrderedBroadcast(intent, null);
}

当我点击接收来自我的应用程序的呼叫时,出现以下日志,请提供解决方案

06-27 12:33:34.153 1543-2055/? W/System.err: java.lang.SecurityException: Nei ther user 10206 nor current process has android.permission.MODIFY_PHONE_STATE. 06-27 12:33:34.153 1543-2055/? W/System.err: at android.app.ContextImpl.enforce(ContextImpl.java:1443) 06-27 12:33:34.154 1543-2055/? W/System.err: at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1475) 06-27 12:33:34.154 1543-2055/? W/System.err: at android.content.ContextWrapper.enforceCallingOrSelfPermission(ContextWrapper.java:675) 06-27 12:33:34.154 1543-2055/? W/System.err: at android.content.ContextWrapper.enforceCallingOrSelfPermission(ContextWrapper.java:675) 06-27 12:33:34.154 1543-2055/? W/System.err: at com.android.phone.PhoneInterfaceManager.enforceModifyPermission(PhoneInterfaceManager.java:1896) 06-27 12:33:34.154 1543-2055/? W/System.err: at com.android.phone.PhoneInterfaceManager.answerRingingCallForSubscriber(PhoneInterfaceManager.java:1199) 06-27 12:33:34.154 1543-2055/? W/System.err: at com.android.phone.PhoneInterfaceManager.answerRingingCall(PhoneInterfaceManager.java:1191) 06-27 12:33:34.154 1543-2055/? W/System.err: at com.android.internal.telephony.ITelephony$Stub.onTransact(ITelephony.java:93) 06-27 12:33:34.154 1543-2055/? W/System.err: at android.os.Binder.execTransact(Binder.java:458) 06-27 12:33:34.155 959-2000/? W/ActivityManager: Permission Denial: not allowed to send broadcast android.intent.action.HEADSET_PLUG from pid=24565, uid=10206 06-27 12:33:34.155 959-2000/? W/System.err: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.HEADSET_PLUG from pid=24565, uid=10206 06-27 12:33:34.155 959-2000/? W/System.err: at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:18158) 06-27 12:33:34.155 959-2000/? W/System.err: at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:18662) 06-27 12:33:34.155 959-2000/? W/System.err: at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:475) 06-27 12:33:34.155 959-2000/? W/System.err: at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2719) 06-27 12:33:34.155 959-2000/? W/System.err: at android.os.Binder.execTransact(Binder.java:458) 06-27 12:33:34.156 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.android.chrome/org.chromium.chrome.browser.media.ui.MediaNotificationManager$PlaybackMediaButtonReceiver requires Intent.android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.157 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.android.chrome/org.chromium.chrome.browser.media.ui.MediaNotificationManager$PresentationMediaButtonReceiver requires Intent.android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.157 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.android.chrome/org.chromium.chrome.browser.media.ui.MediaNotificationManager$CastMediaButtonReceiver requires Intent.android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.157 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.google.android.apps.books/android.support.v4.media.session.MediaButtonReceiver requires Intent.android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.157 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.google.android.youtube/com.google.android.libraries.youtube.player.PlayerUiModule$LegacyMediaButtonIntentReceiver requires Intent.android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.157 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.google.android.music/.playback.MediaButtonIntentReceiver requires Intent.android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.157 959-979/? W/BroadcastQueue: Permission Denial: broadcasting Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } from smartcall.talktime.balance.phone.dialer (pid=24565, uid=10206) is not exported from uid 10103 due to receiver jio.cloud.drive/com.newbay.syncdrive.android.ui.musicplayer.MusicIntentReceiver 06-27 12:33:34.157 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.UCMobile.intl/com.ucmusic.notindex.RemoteControlEventReceiver requires Intent.android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.157 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.jio.media.jiobeats/com.jio.media.mobile.apps.jiobeats.notifications.RemoteViewsReceiver requires Intent.android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.159 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.android.chrome/org.chromium.chrome.browser.media.ui.MediaNotificationManager$PlaybackMediaButtonReceiver requires android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.159 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.android.chrome/org.chromium.chrome.browser.media.ui.MediaNotificationManager$PresentationMediaButtonReceiver requires android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.159 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.android.chrome/org.chromium.chrome.browser.media.ui.MediaNotificationManager$CastMediaButtonReceiver requires android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.159 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.google.android.apps.books/android.support.v4.media.session.MediaButtonReceiver requires android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.160 959-7332/? W/ActivityManager: Permission Denial: not allowed to send broadcast android.intent.action.HEADSET_PLUG from pid=24565, uid=10206 06-27 12:33:34.160 959-7332/? W/System.err: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.HEADSET_PLUG from pid=24565, uid=10206 06-27 12:33:34.160 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.google.android.youtube/com.google.android.libraries.youtube.player.PlayerUiModule$LegacyMediaButtonIntentReceiver requires android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.160 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.google.android.music/.playback.MediaButtonIntentReceiver requires android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.160 959-7332/? W/System.err: at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:18158) 06-27 12:33:34.160 959-979/? W/BroadcastQueue: Permission Denial: broadcasting Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } from smartcall.talktime.balance.phone.dialer (pid=24565, uid=10206) is not exported from uid 10103 due to receiver jio.cloud.drive/com.newbay.syncdrive.android.ui.musicplayer.MusicIntentReceiver 06-27 12:33:34.160 959-7332/? W/System.err: at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:18662) 06-27 12:33:34.160 959-7332/? W/System.err: at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:475) 06-27 12:33:34.160 959-7332/? W/System.err: at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2719) 06-27 12:33:34.160 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.UCMobile.intl/com.ucmusic.notindex.RemoteControlEventReceiver requires android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.160 959-7332/? W/System.err: at android.os.Binder.execTransact(Binder.java:458) 06-27 12:33:34.160 959-979/? W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10 (has extras) } to com.jio.media.jiobeats/com.jio.media.mobile.apps.jiobeats.notifications.RemoteViewsReceiver requires android.permission.CALL_PRIVILEGED due to sender smartcall.talktime.balance.phone.dialer (uid 10206) 06-27 12:33:34.160 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.HEADSET_PLUG from pid=24565, uid=10206 06-27 12:33:34.160 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at android.os.Parcel.readException(Parcel.java:1599) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at android.os.Parcel.readException(Parcel.java:1552) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer

W/System.err: at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:3218) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at android.app.ContextImpl.sendOrderedBroadcast(ContextImpl.java:855) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at android.content.ContextWrapper.sendOrderedBroadcast(ContextWrapper.java:426) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at smartcall.talktime.balance.phone.dialer.callrecorder.activity.FullscreenCallerActivity.m13349i(FullscreenCallerActivity.java:253) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at smartcall.talktime.balance.phone.dialer.callrecorder.activity.FullscreenCallerActivity.m13352k(FullscreenCallerActivity.java:314) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at smartcall.talktime.balance.phone.dialer.callrecorder.activity.FullscreenCallerActivity.access0(FullscreenCallerActivity.java:35) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at smartcall.talktime.balance.phone.dialer.callrecorder.activity.FullscreenCallerActivity.run(FullscreenCallerActivity.java:212) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at android.os.Handler.handleCallback(Handler.java:815) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at android.os.Handler.dispatchMessage(Handler.java:104) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at android.os.Looper.loop(Looper.java:207) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5767) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at java.lang.reflect.Method.invoke(Native Method) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 06-27 12:33:34.161 24565-24565/smartcall.talktime.balance.phone.dialer W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

=========================================== ===========================

我发现此解决方案在某些状态栏中显示呼叫通知的设备上完美运行,此解决方案在 android 安全补丁 2017 年 3 月 1 日 更新后支持 (此解决方案可能不适用于三星和 oppo 或其他设备的某些设备) 仍在寻找完美的解决方案,如果有人找到请更新,谢谢

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import java.util.Arrays;

 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationService extends NotificationListenerService {
public static PendingIntent pendingIntent;

//start service on call notification get
@Override
public void onNotificationPosted(StatusBarNotification sbn) {

    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Log.e("onNotificationSag", "titleaction"+ Arrays.toString(sbn.getNotification().actions));

            if (sbn.getNotification().actions != null) {
                for (Notification.Action action : sbn.getNotification().actions) {
                    Log.e("onNotificationSag", "action" + action.title);
                    if (action.title.toString().equalsIgnoreCase("Answer")) {
                        pendingIntent = action.actionIntent;
                        //  try {
                        //receive call automatically
                        pendingIntent.send();

                        //} catch (PendingIntent.CanceledException e) {
                        //     e.printStackTrace();
                        //}

                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {

} 
}

将以下权限和代码添加到清单

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.service.notification.NotificationListenerService" />
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />



<service
        android:name=".NotificationService "
        android:label="@string/app_name"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action
                android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>