如何检测android中的来电状态

And*_*era 0 android android-layout

有没有什么办法,使我们可以检测如Android来电的状态TelephonyManager.CALL_STATE_RINGINGCALL_STATE_IDLE.
如果来电已接听,那么它在TELEPHONY MANAGER API中的状态
如果错过了未接听的来电,那么它在TELEPHONY MANAGER API中的状态是什么.
任何人都可以帮我解决这个问题.
提前致谢

小智 6

这就是我做的

清单文件:

    <receiver android:name="com.xxx.xxx.zzz.IncomingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"></action>
        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)

我的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);
}
Run Code Online (Sandbox Code Playgroud)