I use Broadcast receiver to catch Phone state Change. It works fine when the state change in in first time (for State_OffHook), but don't react when the call ends. This is my code:
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {working fine}
el开发者_开发问答se if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {doesn't react}
When you have no call you are in IDLE
state and when you get a call it goes to OFFHOOK
state
and when your call ends it again goes to IDLE
state
for more info refer this
How to know whether I am in a call on Android?
EDIT:
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show();
if(UDF.phoneState != TelephonyManager.CALL_STATE_IDLE) {
//Here you are came from offhook because value of UDF.phoneState != TelephonyManager.CALL_STATE_IDLE
//IDLE is calls many times so you have to keep track by a static variable like UDF.phoneState
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
//Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show();
endCallIfBlocked(incomingNumber);
break;
default:
break;
}
UDF.phoneState = state;
}
精彩评论