我们是否可以通过任何方式检测Android中的来电状态,例如TelephonyManager.CALL_STATE_RINGING
或CALL_STATE_IDLE
。
如果来电被接听,那么它在TELEPHONY MANAGER API中的状态是什么
如果错过了未接听的来电,那么它在TELEPHONY MANAGER API中的状态是什么。
任何人都可以帮我解决这个问题。
在此先感谢
答案 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
注册接收器。