10

I know that we can listen phone state by broadcast receiver with filter

action android:name="android.intent.action.PHONE_STATE"

but this way requires permission

uses-permission android:name="android.permission.READ_PHONE_STATE"

How to determine the incoming GSM call whithout this permission?

P.S.

I'm sure that this is possibly because Whatsapp doing it.
When I'm talking on the Whatsapp audio calls and then got incoming gsm call - whatsapp checking it and set self call to pause. How do they understand that need set pause?

Whatsapp has READ_PHONE_STATE permission in Manifest file, but I check calls on Android 6 and not granted this permission. This permission was disabled.

When this permission is disabled my Receiver doesn't receive action android.intent.action.PHONE_STATE and PhoneStateListener is not working.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dmitriy Puchkov
  • 1,530
  • 17
  • 41
  • `I need some mechanics` - then write your code and come back if you face any real problems. Begging for code in disguise of bounty is pathetic. – Marcin Orlowski Mar 28 '17 at 10:17

5 Answers5

15

I found solution without this permission.

First of all (when our sip call start) we need request audio focus with lisener

 mAudioManager.requestAudioFocus(afChangeListener, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);

The afChangeListener can tell us then someboby changed audio focus and then we can check current audio channel. If current mode is AudioManager.MODE_IN_CALL or AudioManager.MODE_RINGTONE then we know that gsm call started.

AudioManager.OnAudioFocusChangeListener afChangeListener =
        new AudioManager.OnAudioFocusChangeListener() {
            public void onAudioFocusChange(int focusChange) {

                if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                    // Permanent loss of audio focus
                    // Pause playback immediately
                } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                    ScheduledExecutorService scheduler = App.getInstance().obtainScheduler();
                    scheduler.schedule(() -> {
                         Logging.i(TAG, "onAudioFocusChange defer mode = " + mAudioManager.getMode());
                         switch (mAudioManager.getMode()) {
                             case AudioManager.MODE_RINGTONE:
                             case AudioManager.MODE_IN_CALL:
                                 //**there we can start standby mode**
                                 break;
                         }
                    }, 100, TimeUnit.MILLISECONDS);

                } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
                    Logging.i(TAG, "onAudioFocusChange AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
                    // Lower the volume, keep playing
                } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                    // Your app has been granted audio focus again
                    //**there we can stop standby mode**
                }

            }
        };
Dmitriy Puchkov
  • 1,530
  • 17
  • 41
1

First of all whatsapp used permission which required to get phone state

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Here is WhatsApp menifest file link.

You can see their they have added their and used inside it.

Android itself provide best solution to handle it using broadcast receiver.You should use these otherwise your system would be hacky.

Follow these link to work with broadcast receiver to detect phone calls.

Hope these helps you.

Community
  • 1
  • 1
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

When the android device receives a phone call onPause of Activity which was being displayed before the phone call gets automatically called and you can pause the call there.

Karan Kalsi
  • 789
  • 3
  • 21
0

You can get this with onReceive method with phone number and all details with incoming call.

parik dhakan
  • 787
  • 4
  • 19
0

Simply Short Answer:

1- Check incoming call function

private boolean isIncomingCall(Context context) {
    AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    return manager.getMode() == AudioManager.MODE_IN_CALL || manager.getMode() == AudioManager.MODE_RINGTONE;
}

2- Handler repeat itself every 2 seconds

Handler handler = new Handler();
Runnable runnableCheckOngoingCall;

   runnableCheckOngoingCall = new Runnable() {
        @Override
        public void run() {
            if (handler != null) {
                if (isIncomingCall(ActivityName.this)) {

                    // Write your logic code here

                    handler.removeCallbacks(runnableCheckOngoingCall);
                } else {
                    handler.postDelayed(runnableCheckOngoingCall, 2000);
                }
            }

        }
    };

 handler.postDelayed(runnableCheckOngoingCall, 2000);

3- Don't forget to remove handler callback when destroy your activity

@Override
public void onDestroy() {
    super.onDestroy();
    if (handler != null)  handler.removeCallbacks(runnableCheckOngoingCall);
}
Islam Ahmed
  • 668
  • 9
  • 19