问题
I have the following code and I have read that for outgoing calls ringing state is active when phone call between two parties is connected and other party can actually see that you are calling and answer your call. Idle State is when call is disconnected and OFF_HOOK state is the moment when you press the button to dial.I am getting in OFF_HOOK and IDLE state but can't get in ringing state .What might be the problem? .Any help will be appreciated.
public void onReceive(final Context context, Intent intent) {
TelephonyManager Tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
Tm.listen(new PhoneStateListener(){
public void onCallStateChanged(int state,String number) {
super.onCallStateChanged(state, number);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//when Idle i.e no call
Toast.makeText(context, "Phone state Idle", 1).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//when Off hook i.e in call
//Make intent and start your service here
Toast.makeText(context, "Phone state Off hook", 1).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(context, "Phone state Ringing", 1).show();
break;
default:
break;
}
}
},PhoneStateListener.LISTEN_CALL_STATE);
回答1:
TelephonyManager.CALL_STATE_RINGING
is meant for the incoming call. Its not for the ringing of other side for an outgoing call.
TelephonyManager.CALL_STATE_RINGING
will be triggered when you have an incoming call and your phone is ringing.
and also for your information. There is no public api to detect the ringing state of other side for outgoing call.
回答2:
Maybe try out this helper class i copied somewhere online, cannot remember where
/*Helper class to detect incoming and outgoing calls.*/
public class CallHelper {
private Activity activity;
private TelephonyManager tm;
private CallStateListener callStateListener;
private OutgoingReceiver outgoingReceiver;
public String TelephoneNumber;
public boolean InCall = false;
public CallHelper(Activity activity) {
this.activity = activity;
callStateListener = new CallStateListener();
outgoingReceiver = new OutgoingReceiver();
}
/*Listener to detect incoming calls.*/
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: {
TelephoneNumber = incomingNumber;
inout = "in";
InCall = false;
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
if (!InCall) {
InCall = true;
//Do your stuff here when in call...
}
break;
}
case TelephonyManager.CALL_STATE_IDLE: {
InCall = false;
break;
}
default: {
InCall = false;
break;
}
}
}
}
/*Broadcast receiver to detect the outgoing calls.*/
public class OutgoingReceiver extends BroadcastReceiver {
public OutgoingReceiver() { }
@Override
public void onReceive(Context context, Intent intent) {
TelephoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
}
/*Start calls detection.*/
public void start() {
tm = (TelephonyManager) activity.getSystemService(Activity.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
activity.registerReceiver(outgoingReceiver, intentFilter);
}
/*Stop calls detection.*/
public void stop() {
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
activity.unregisterReceiver(outgoingReceiver);
}
}
来源:https://stackoverflow.com/questions/19509292/ringing-state-out-going-calls