There are a few things you can do:
First of all, you can listen for changes in call state using PhoneStateListener . You can register a listener in TelephonyManager:
PhoneStateListener phoneStateListener = new PhoneStateListener() { @Override public void onCallStateChanged(int state, String incomingNumber) { if (state == TelephonyManager.CALL_STATE_RINGING) { //Incoming call: Pause music } else if(state == TelephonyManager.CALL_STATE_IDLE) { //Not in call: Play music } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) { //A call is dialing, active or on hold } super.onCallStateChanged(state, incomingNumber); } }; TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if(mgr != null) { mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); }
Remember to unregister the listener when it is no longer needed using PhoneStateListener.LISTEN_NONE :
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if(mgr != null) { mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); }
Read the documentation for more details .
Another thing you can do is listen to the android.intent.action.PHONE_STATE broadcast. It will contain an optional TelephonyManager.EXTRA_STATE that will give you call information. Check out the documentation here .
Please note that in both cases you will need android.permission.READ_PHONE_STATE transmission.
Kaloer Apr 10 2018-11-11T00: 00Z
source share