如何检测android中的来电状态

时间:2011-12-17 19:54:23

标签: android android-layout

我们是否可以通过任何方式检测Android中的来电状态,例如TelephonyManager.CALL_STATE_RINGINGCALL_STATE_IDLE
如果来电被接听,那么它在TELEPHONY MANAGER API中的状态是什么 如果错过了未接听的来电,那么它在TELEPHONY MANAGER API中的状态是什么。
任何人都可以帮我解决这个问题。
在此先感谢

2 个答案:

答案 0 :(得分:6)

这就是我做的

清单文件

    <receiver android:name="com.xxx.xxx.zzz.IncomingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"></action>
        </intent-filter>
    </receiver>
我的 BroadcastReceiver(IncomingCallReceiver)中的

字段

private static String previousCallingNumber = null;
private static String previousState = null;
private String RINGING = TelephonyManager.EXTRA_STATE_RINGING;
private String OFFHOOOK = TelephonyManager.EXTRA_STATE_OFFHOOK;
private String IDLE = TelephonyManager.EXTRA_STATE_IDLE;

public void onReceive(final Context context,Intent intent) {


    final Bundle extras = intent.getExtras();
    String state= null;

    if (extras != null)
    {
        state = extras.getString(TelephonyManager.EXTRA_STATE);
    }

    /**
         * This part of the code records that a call was recieved/attended or missed.
         * 
         */
        else if (state!=null &&  state.equals(OFFHOOOK))
        {

            if(previousState!=null)
            {

                        if(previousState.equals(RINGING) && previousCallingNumber!=null)
                        {
                           /**
                             *   received call, the ringing call has been attended. 
                             */
                         String msisdn = MSISDNPreFixHandler.fixMsisdn(previousCallingNumber);


                         Log.i("IncomingCallReceiver", "Incoming Received Call Saved: "+msisdn);


                       }

            }
            /**
             * Else the Incoming Call receiver is triggered when an outgoing call is made.
             * 
             */

        }
        else if (state!=null &&  state.equals(IDLE))
        {

                String incomingNumberForMissedCall = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                String msisdn = MSISDNPreFixHandler.fixMsisdn(incomingNumberForMissedCall);

                if(incomingNumberForMissedCall!=null)
                {                      
                   /**
                     *   missed call, the ringing call has been missed. 
                     */



                     Log.i("IncomingCallReceiver", "Missed Call Saved: "+msisdn);


                }

         }


    /**
     *   Very important to keep the previous state & number in cache , 
     *   as through it we can recognize the received attended call.
     * 
     */

    previousState = state;
    previousCallingNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}

答案 1 :(得分:2)

要在调用进入时获取状态,需要创建BroadcastReceiver来处理ACTION_ANSWER意图。

您还可以为ACTION_PHONE_STATE_CHANGED添加BroadcastReceiver,并观看TelephonyManager呼叫状态更改。我不完全确定通话期间通话状态的行为或错过了,但您可以使用BroadcastReceiver快速进行实验。

不要忘记在AndroidManifest.xml注册接收器。