问题
I'm developing on android framework,
I want to fire an event when an outgoing call is received by the callee , and also when the call is ended (from any of the two sides)
回答1:
Inorder to know wheter the calling party has recieved the call, you will need to create a listener.
class PhoneInfo extends BroadcastReceiver {
/**
* Getting the System Telephony Service and registering a listener for Voice Call state
*/
@Override
public void onReceive(Context context, Intent intent) {
IncomingCallListener phoneListener = new IncomingCallListener();
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
class IncomingCallListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
Log.i(logcat,"CALL_STATE changed " + callflag);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.i(logcat,"CALL_STATE_IDLE");
//This is where call ends.
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//This is where we know call is established
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.i(logcat,"CALL_STATE_RINGING");
break;
}
}
}
Register this with your activity as
phoneInfo = new PhoneInfo(this);
registerReceiver(phoneInfo, new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL));
Now with Logs u can see how states are changed when a call is dialed or received.
来源:https://stackoverflow.com/questions/12913267/android-check-when-outgoing-call-is-received-by-callee