是否可以拦截Android应用程序中的电话呼叫?

时间:2014-08-28 07:24:15

标签: android

我正在研究是否可以接收来电和拦截应用程序内的呼叫,以便在我们接听电话之前,我们可以开始在后台播放一些音乐,以便来电者将音乐视为好。

2 个答案:

答案 0 :(得分:3)

当然可以!您必须创建侦听传入呼叫意图的服务,然后执行您喜欢的操作。

有关详细信息,请参阅本教程:

答案 1 :(得分:0)

您可以使用广播接收器

<receiver android:name=".IncomingCall">   
   <intent-filter android:priority="999"> //high priority, so that your receiver is called soon
    <action android:name="android.intent.action.PHONE_STATE" />
   </intent-filter>
 </receiver>

添加清单文件的权限

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

代码:

public class IncomingCall extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

    try {
               // TELEPHONY MANAGER class object to register one listner
                TelephonyManager tmgr = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);

                //Create Listner
                MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

                // Register listener for LISTEN_CALL_STATE
                tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        } catch (Exception e) {
            Log.e("Phone Receive Error", " " + e);
        }

    }

    private class MyPhoneStateListener extends PhoneStateListener {

        public void onCallStateChanged(int state, String incomingNumber) {

            Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);

            if (state == 1) {

                String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(pcontext, msg, duration);
                toast.show();

            }
        }
    }
}