5

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?

cytinus
  • 5,467
  • 8
  • 36
  • 47
Jatin Barot
  • 115
  • 1
  • 9

2 Answers2

4

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" />
krains
  • 93
  • 1
  • 4
  • Hello krains, i want to check incoming call state when my activity is running at that moment i want my application go in background and keep pause than after call is end my application automatically resume at that level when it paused. – Jatin Barot Mar 31 '12 at 11:44
  • I am not sure if it works but you might have a look here [android dev page](http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP) and add only the FLAG_ACTIVITY_SINGLE_TOP Flag to the intent. – krains Mar 31 '12 at 11:56
1
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());
}
juan.facorro
  • 9,791
  • 2
  • 33
  • 41
Ishu
  • 5,357
  • 4
  • 16
  • 17