Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Android application pause on incoming call and resume after call end

Tags:

android

I want to pause my android application when the phone receives an incoming call. After the call ends, I want my applications to resume automatically.

How would this be implemented in an Android application?

like image 577
Jatin Barot Avatar asked Mar 31 '12 10:03

Jatin Barot


People also ask

How do you pause and resume activity on Android?

During an activity, press and hold left to stop the clock and pause your activity. To restart the activity again, press right.

What is pause method in Android?

What is pause method in Android? App pause is a feature in Android Q which allows us as users to pause a specific application for that day. Once an application is in pause state, you will not receive notifications from the app for the rest of the day.22-Jun-2019.

When a migration activity is paused by the user what methods can be used to resume the activity?

Resume Your Activity When the user resumes your activity from the Paused state, the system calls the onResume() method. Be aware that the system calls this method every time your activity comes into the foreground, including when it's created for the first time.

How do I stop music playing during calls?

Android: This is completely up to you. Under the Settings tab, you can check the option "Pause During Call" and the music will Pause if you receive a call. Otherwise, leave this box un-checked and your music will continue to play if your phone rings.


2 Answers

you have to implement a Listener for the PhoneState. I did this in a private Class:

private class PhoneCallListener extends PhoneStateListener {

    private boolean isPhoneCalling = false;

    // needed for logging
    String TAG = "PhoneCallListener";

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

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            Log.i(TAG, "RINGING, number: " + incomingNumber);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            Log.i(TAG, "OFFHOOK");

            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // run when class initial and phone call ended,
            // need detect flag from CALL_STATE_OFFHOOK
            Log.i(TAG, "IDLE");

            if (isPhoneCalling) {

                Log.i(TAG, "restart app");

                // restart call application
                Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(
                                getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(i);

                isPhoneCalling = false;
            }

        }


}
}

and you need to add the permission to the Manifest-File

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
like image 168
krains Avatar answered Oct 25 '22 06:10

krains


private class EndCallListener extends PhoneStateListener {
  private boolean active = false;
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
    if(TelephonyManager.CALL_STATE_RINGING == state) {
      Log.i("EndCallListener", "RINGING, number: " + incomingNumber);
    }
    if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
      //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
      active = true;
      Log.i("EndCallListener", "OFFHOOK");
    }
    if(TelephonyManager.CALL_STATE_IDLE == state) {
      //when this state occurs, and your flag is set, restart your app
      Log.i("EndCallListener", "IDLE");
      if (active) {
        active = false;
        // stop listening                   
        TelephonyManager mTM = (TelephonyManager) m_activity.getSystemService( Context.TELEPHONY_SERVICE );
        mTM.listen(this, PhoneStateListener.LISTEN_NONE);
        // restart the inbox activity
        //Intent intent = new Intent(m_activity, MDInboxActivity.class);
        //m_activity.startActivity(intent);
      }
    }
  }
}

And you can initialize the above class by calling the below lines:

try {
  EndCallListener callListener = new EndCallListener();
  TelephonyManager mTM = (TelephonyManager) m_activity.getSystemService(Context.TELEPHONY_SERVICE);
  mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch(Exception e) {
  Log.e("callMonitor", "Exception: "+e.toString());
}
like image 32
Ishu Avatar answered Oct 25 '22 04:10

Ishu