看看意图 NEW_OUTGOING_CALL
<receiver android:name=".receiver.OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
然后您可以使用 PhoneStateListener 检查通话状态:
public class OutgoingCallReceiver extends BroadcastReceiver {
protected static final String CLASS_TAG = "OutgoingCallReceiver : ";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle) return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(LOG_TAG, CLASS_TAG + "phone number " + phonenumber);
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(LOG_TAG, CLASS_TAG + "RINGING");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(LOG_TAG, CLASS_TAG + "OFFHOOK");
Process.onCallStateIsOFFHOOK();
break;
case TelephonyManager.CALL_STATE_IDLE:
Process.onCallStateIsIDLE();
Log.d(LOG_TAG, CLASS_TAG + "IDLE");
break;
default:
Log.d(LOG_TAG, CLASS_TAG + "Default: " + state);
break;
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
}
在 Process 类中,您可以通过录制 VOICE_CALL 来检测铃声并创建更多状态,例如 CALLBEGINS、CALLANSWERED。onCallStateIsIDLE() 示例:
public void onCallStateIsIDLE() {
setSpeakingOn(false);
if (stateCall == STATE_CALLANSWERED ||
stateCall == STATE_CALLBEGINS ||
stateCall == STATE_DIALISOFFHOOK ||
stateCall == STATE_DIALENDREQUESTED) {
Log.i(LOG_TAG, CLASS_TAG + " - forcing state : STATE_DIALREADY : old state = " + stateCall);
stateCall = STATE_DIALREADY;
}
}