2

I need to accept automatically phone calls (depending on source phone number). At this time, I can end incoming calls, but can't accept'em.

I've found several examples and this is what I have at this moment.

Add to AndroidManifest:

 <permission android:name="android.permission.MODIFY_PHONE_STATE" />
 <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
 <uses-permission android:name="android.permission.CALL_PHONE" />
 ...
 <receiver android:name=".panic.IncomingCallReceiver">
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

And this is my code:

public class IncomingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        MyPhoneStateListener listener = new MyPhoneStateListener(context);
        telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    public class MyPhoneStateListener extends PhoneStateListener {
        Context context;

        public MyPhoneStateListener(Context context) {
            super();
            this.context = context;
        }

        @Override
        public void onCallStateChanged(int state, String callingNumber) {
            super.onCallStateChanged(state, callingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    break;

                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //handle out going call
                    acceptCallIfBlocked(callingNumber);
                    break;

                case TelephonyManager.CALL_STATE_RINGING:
                    //handle in coming call
                    acceptCallIfBlocked(callingNumber);
                    break;

                default:
                    break;
            }
        }

        private void acceptCallIfBlocked(String callingNumber) {
            if (shouldAcceptCall()) {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                try {
                    // Java reflection to gain access to TelephonyManager's
                    // ITelephony getter
                    Class c = Class.forName(tm.getClass().getName());
                    Method m = c.getDeclaredMethod("getITelephony");
                    m.setAccessible(true);
                    final Object invoke = m.invoke(tm);
                    c = Class.forName(invoke.getClass().getName());
                    m = c.getDeclaredMethod("endCall");
//            m = c.getDeclaredMethod("answerRingingCall"); //  NOT WORKING
                    m.setAccessible(true); // Make it accessible
                    final Object invoke1 = m.invoke(invoke);
                } catch (Throwable ignored) {
                }
            }
        }
    }
}

I can successfully end calls with this code, but I need to execute answerRingingCall in order to acomplish what I need.

I'm getting this exception:

Neither user 10056 nor current process has android.permission.MODIFY_PHONE_STATE

I'm running on Lollipop but need to execute in as many devices as possible. Any help?

MatheusJardimB
  • 3,599
  • 7
  • 46
  • 70
  • possible duplicate of [How to programatically answer/end a call in Android 4.1?](http://stackoverflow.com/questions/15481524/how-to-programatically-answer-end-a-call-in-android-4-1) – Codeman Feb 16 '15 at 21:19
  • @Pheonixblade9 I've tried the solution you pointed, but does not work – MatheusJardimB Feb 16 '15 at 21:27
  • The point is, there is no solution. This behavior is blocked on purpose to prevent malicious apps from stealing your phone calls. – Codeman Feb 16 '15 at 21:27
  • I see, unfortunatelly I'm trying to build a 'panic button' app in order to save people. :/ Thanks anyway – MatheusJardimB Feb 16 '15 at 21:29

1 Answers1

6

Intercepting calls only works from 2.2 - 4.0. Google has blocked the MODIFY_PHONE_STATE permission for anyone except for phone manufacturer certificates

Google has restricted intercepting calls as a security feature. In short, you're not allowed to do this on purpose.

You can see it here in the Android developer documentation:

public static final String MODIFY_PHONE_STATE

Added in API level 1
Allows modification of the telephony state - power on, mmi, etc. Does not include placing calls.

Not for use by third-party applications.

Constant Value: "android.permission.MODIFY_PHONE_STATE"

Note the phrase Not for use by third-party applications.

Codeman
  • 12,157
  • 10
  • 53
  • 91