androidphone-calltelephonytelephonymanagerphone-state-listener

ITelephony interface method answerRingingCall() not working


I want to receive Incoming call from my application, I am using ITelephony.aidl to End and receive call. I have written the following code to answer the call on button press, mostly I am able to end the call on all the devices but when I try to receive a call it's not working.

This (call answer) code is working fine on some devices that I have tested on (Motorola** e3 6.0 android security patch 1 Jun 2016 , honor 4.4.4, YU 4.4.4. etc)it's not working on(Xiaomi note 4 6.0.1, lyf 6.O.1 android security patch 1 march 2017)` may be the issue is causing because of android security patch update on latest versions.

I think google has allowed some permission only for system apps for security reason, in latest Update of android security patch 1 Jun 2016 to 1 March 2017.

Please help, Thank you

    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);
}

I get below log when I click to receive call from my application please provide solution

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.access$200(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$2.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)

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


Solution

  • I have found this solution works perfectly on some devices which shows call notification in status bar ,this solution supports after android security patch 1 march 2017 update (this solution may not work on some devices of Samsung and oppo or other) still looking for perfect solution if any one finds please update , thank you

    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) {
    
    } 
    }
    

    add following permissions and code to manifest

    <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>