接听电话

时间:2015-02-16 21:09:40

标签: android telephonymanager

我需要自动接听电话(取决于来源电话号码)。这时,我可以结束来电,但不能接受'。

我找到了几个例子,这就是我现在所拥有的。

添加到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>

这是我的代码:

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

我可以使用此代码成功结束调用,但我需要执行answerRingingCall才能完成我需要的内容。

我遇到了这个例外:

  

用户10056和当前进程都没有   android.permission.MODIFY_PHONE_STATE

我在Lollipop上运行,但需要在尽可能多的设备中执行。 有什么帮助吗?

1 个答案:

答案 0 :(得分:6)

拦截电话仅适用于2.2 - 4.0。 Google has blocked the MODIFY_PHONE_STATE permission for anyone except for phone manufacturer certificates

Google已将限制拦截电话限制为安全功能。简而言之,您不能故意这样做。

您可以在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"

请注意短语不供第三方应用程序使用。