I am beginner in android and developing an android application to monitor the network status when the user is on call(for both incoming and outgoing calls)...
i can monitor the network status .for incoming calls by implementing the PhonestateListner class and overriding the onCallStateChanged method, i am using the constants (CALL_STATE_IDLE,CALL_STATE_OFFHOOK and CALL_STATE_RINGING) defined in the TelephonyManager class inside the onCallStateChanged method , but these constants are not working properly for outgoing calls...
my question is
please help me to understand this concept... Thanks for your time
Below code help you to get net work status code in onCreate
StateListener phoneStateListener = new StateListener();
TelephonyManager telephonyManager =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
create an inner class for listening state of phone.
class StateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("call Activity off hook");
LockScreenActivity.this.finish();
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
};
Set permission in manifest
file
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
BroadcastReceiver with action for outgoing call android.intent.action.NEW_OUTGOING_CALL
and android.intent.action.PHONE_STATE
.
The android.intent.action.NEW_OUTGOING_CALL
will be broadcasted when an outgoing call is initiated. The receiving intent will have an extra string variable Intent.EXTRA_PHONE_NUMBER
which contains the outgoing number. This requires the permission android.permission.PROCESS_OUTGOING_CALLS
.
To detect the incoming call, register a BroadcastReceiver for the action android.intent.action.PHONE_STATE
. This will be broadcasted when there is a change in phone state. The receiving intent will have an extra string variable TelephonyManager.EXTRA_STATE
which describes the phone state. If this state is TelephonyManager.EXTRA_STATE_RINGING
then there will be another extra string variable TelephonyManager.EXTRA_INCOMING_NUMBER
. This variable contains the incoming phone number. Note that this variable will not be present when the state is not TelephonyManager.EXTRA_STATE_RINGING
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With