简体   繁体   English

如何在Android中检测来电状态

[英]How to detect incoming call status in Android

Is there any way in which we can detect status of incoming call in android like TelephonyManager.CALL_STATE_RINGING or CALL_STATE_IDLE .有什么方法可以检测 android 中来电的状态,例如TelephonyManager.CALL_STATE_RINGINGCALL_STATE_IDLE If the incoming call is answered then what is its status in TELEPHONY MANAGER API如果来电被接听,那么它在 TELEPHONY MANAGER API 中的状态是什么

If the incoming call is missed that is not answered then what is its status in TELEPHONY MANAGER API.如果未接听未接来电,那么它在 TELEPHONY MANAGER API 中的状态是什么。 What can I do to resolve this issue?我能做些什么来解决这个问题?

This is what i did这就是我所做的

Manifest File :清单文件

    <receiver android:name="com.xxx.xxx.zzz.IncomingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"></action>
        </intent-filter>
    </receiver>

fields in my BroadcastReceiver(IncomingCallReceiver)我的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);
}

To get the state when a call comes in need to create a BroadcastReceiver to handle the ACTION_ANSWER intent.要在有呼叫时获取状态,需要创建一个BroadcastReceiver来处理ACTION_ANSWER意图。

You can also add a BroadcastReceiver for the ACTION_PHONE_STATE_CHANGED and watch as the TelephonyManager Call State changes.您还可以为ACTION_PHONE_STATE_CHANGED添加一个BroadcastReceiver并观察TelephonyManager Call State 的变化。 I'm not entirely sure the behavior of the call status during the call or if missed, but you can pretty quickly experiment with it using BroadcastReceiver .我不完全确定通话期间或未接听通话状态的行为,但您可以使用BroadcastReceiver很快地对其进行试验。

Don't forget to register the receivers in your AndroidManifest.xml .不要忘记在您的AndroidManifest.xml注册接收器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM