Answer phone call

偶尔善良 提交于 2019-12-06 11:45:58

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/28550390/answer-phone-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!