Capture incoming call. Android

4

I have my application that uses the MediaPlayer class, I need that when a call comes in the sound is mute, but when the app goes to the background (when you press the home button for example) follow the playback of the audio.

   public static class ReceptorLlamadas extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
       call(context);
   }

   private void call(Context context) {
       PhoneCallListener phoneListener = new PhoneCallListener();
       TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
   }

   private class PhoneCallListener extends PhoneStateListener {
       public boolean isPhoneCalling = false;
       Boolean wasRinging = false;

       @Override
       public void onCallStateChanged(int state, String incomingNumber) {
           if (TelephonyManager.CALL_STATE_RINGING == state) {
               // phone ringing
              //Aquí ya detectas que el teléfono esta recibiendo una llamada entrante

           }
           if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
               // active
               isPhoneCalling = true;
               if (mPlayer != null && mPlayer.isPlaying()) {
                   mPlayer.setVolume(0,0);
               }
           }

           if (TelephonyManager.CALL_STATE_IDLE == state) {

               isPhoneCalling = false;
           }

       }
   }

and this I added to the manifest.xml

<receiver android:name=".MainActivity$ReceptorLlamadas">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
 </receiver>
    
asked by Rodrigo 19.07.2016 в 01:13
source

1 answer

2

With TelephonyManager you can collect the different states of the phone:

 public class prueba extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    call(context);
}

private void call(Context context) {
    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}

private class PhoneCallListener extends PhoneStateListener {
    public boolean isPhoneCalling = false;
    Boolean wasRinging = false;

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            //Aquí ya detectas que el teléfono esta recibiendo una llamada entrante
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {

            isPhoneCalling = false;
        }

    }
}

}

    
answered by 29.07.2016 / 19:44
source