Please tell me how when an incoming call to reduce the sound in the application. There is a player that plays audio, it is necessary that when an incoming call, the sound is automatically reduced, and after the end of the call is restored. I read that you need to use telephonymanager, but did not quite understand how. Can I have a little example?

public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private BroadcastReceiver receiver; private TelephonyManager tm; TextView callState; CallStateListener callStateListener; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); callStateListener = new CallStateListener(); tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE); callState = (TextView) findViewById(R.id.callState); receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE); if (phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING) || phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { ringing(); } else if (phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)) { notRinging(); } } } }; IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.PHONE_STATE"); registerReceiver(receiver,filter); } @Override protected void onPause() { super.onPause(); tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE); } private class CallStateListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_RINGING: callState.setText("Is Ringing"); break; case TelephonyManager.CALL_STATE_IDLE: callState.setText("Not ringing"); break; } } } private void notRinging() { // дСйствия, ΠΊΠΎΠ³Π΄Π° Ρ‚Π΅Π»Π΅Ρ„ΠΎΠ½ Π·Π°ΠΊΠΎΠ½Ρ‡ΠΈΠ» Π·Π²ΠΎΠ½ΠΎΠΊ Log.i(TAG, "notRinging: "); callState.setText("notRinging"); } private void ringing (){ // дСйствия, ΠΊΠΎΠ³Π΄Π° Ρ‚Π΅Π»Π΅Ρ„ΠΎΠ½ Π·Π²ΠΎΠ½ΠΈΡ‚ ΠΈΠ»ΠΈ ΠΏΠΎ Π½Π΅ΠΌΡƒ Ρ€Π°Π·Π³ΠΎΠ²Π°Ρ€ΠΈΠ²Π°ΡŽΡ‚ Log.i(TAG, "ringing: "); callState.setText("ringing"); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(receiver); } } 
  • What a strange code and repeated for the third time, where do you take it? or you, Natalya and Alex are one and the same person ... In this answer there is a working code with a response to an incoming call. Instead of displaying messages, do in the right places increase or decrease the volume - pavlofff
  • I found this code in this answer. And I decided to use it as it is for now to see whether these messages appear in textEdit. I honestly did not look at your answer there. ie, after the case TelephonyManager.CALL_STATE_RINGING: callState.setText ("Is Ringing"); can I write code to reduce the sound and everything will work? - user215435
  • Works! In that question, you write that it will only work when the activation is in the foreground. And how to make it work and when the activity is not in the foreground? those. If I press the "home" button the application collapses, but continues to play. As in this case - user215435
  • In general, if the response from the comment above cancels the registration of the listener - tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE); remove from onPause() and transfer to onDestroy() then this code will have to work until the activation is destroyed, that is, even if it is not in the foreground. - pavlofff

1 answer 1

In order to perform certain actions when the phone is ringing or talking on it, regardless of whether it is activated in the foreground or not, it is necessary to register the receiver for the call event and activate the registration of the receiver when the activation is destroyed:

 public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private BroadcastReceiver receiver; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE); if (phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING) || phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { ringing(); } else if (phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)) { notRinging(); } } } }; IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.PHONE_STATE"); registerReceiver(receiver,filter); } private void notRinging() { // дСйствия, ΠΊΠΎΠ³Π΄Π° Ρ‚Π΅Π»Π΅Ρ„ΠΎΠ½ Π·Π°ΠΊΠΎΠ½Ρ‡ΠΈΠ» Π·Π²ΠΎΠ½ΠΎΠΊ Log.i(TAG, "notRinging: "); } private void ringing (){ // дСйствия, ΠΊΠΎΠ³Π΄Π° Ρ‚Π΅Π»Π΅Ρ„ΠΎΠ½ Π·Π²ΠΎΠ½ΠΈΡ‚ ΠΈΠ»ΠΈ ΠΏΠΎ Π½Π΅ΠΌΡƒ Ρ€Π°Π·Π³ΠΎΠ²Π°Ρ€ΠΈΠ²Π°ΡŽΡ‚ Log.i(TAG, "ringing: "); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(receiver); } } 

Add permission to manifest:

 <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
  • those. In the notRinging () and ringing () methods, do I write code to reduce and increase the sound and will it work? I'm just now I can not test with the phone. - user215435
  • I updated my question and inserted what I did. If the activation is not in the foreground did not work. - user215435
  • I tried it too. I copied the code and added callState.setText ("notRinging") to the method; When I start the application, I understand that TextView should be notRinging, but nothing is displayed there. - user215435
  • When the receiver is running, the "notRinging" will be after a reset / cancel / end a call, and not when the application is started - this is not a listener for the event, but an action for notification. At start no notification comes, because no call is completed. The comment in the method is written when it is called. When dialing \ conversation will be "ringing" - pavlofff
  • tried it now. Did not work. When activating in the foreground and when not, it didn't work either. NotRinging should also appear in the log, nothing happens. In the manifest, you do not need to prescribe anything extra? - user215435