how to get only active calls from PhoneStateListener
I would like to get only active calls from PhoneStateListener. I have created the listener with method onCallStateChanged. Once I dial (and the call is picked up) the state is already off-hook.
Well, it doesn't really surprise me, because of the text in the API: Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
But is there a possibility to get only acti开发者_运维问答ve calls?
From what I tried on my nexus one, active call wake CALL_STATE_OFFHOOK, try use log file to watch the steps.
Try this ..
private PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "ringing goes here");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "Active call goes here");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "CALL STATE IDLE goes here");
break;
default:
Log.d(TAG, "Unknown state=" + state);
}
}
and then call it :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
Don't forget add permission to your Mainifest file:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
精彩评论