3

Currently when I run my application and if phone rings , phone get preference and my application is killed. Is there any way either my application takes a preference i.e. let phone call to hit voice mail or shift my app to background for short period of time , till user take a call , and bring back to foreground once he complete. thanks

Namit
  • 331
  • 3
  • 7
  • +1 it stops my gps too – rpax May 23 '14 at 06:00
  • List for phonestate and when received, call your activity on pause() or do whatever you need to – Saqib May 23 '14 at 06:08
  • 1
    saqib - can you please explain this approach – Namit May 23 '14 at 13:44
  • The [android developer documentation](http://developer.android.com/training/basics/activity-lifecycle/stopping.html) explains this process in great detail. For more information also read the [Activity](http://developer.android.com/reference/android/app/Activity.html) documentation. – free3dom May 24 '14 at 07:40

3 Answers3

2

you can do one thing. You can pause your application during the incoming call and after that, resume the application from the same place. I know this is not the exact solution of your issue but somehow, it will reduce your work load. hope this will help.

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 also add this permission to manifest.xml file

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Devraj
  • 1,479
  • 1
  • 22
  • 42
1

I think this is the default feature of android that any application go inactive if incoming call is in active. We cannot change this.

While the user is in the phone call, though, they can change to another app simply by pressing the home button and starting another app from the home screen, or by double-pressing the home button and switching to another app, including yours.

Pang
  • 9,564
  • 146
  • 81
  • 122
Devraj
  • 1,479
  • 1
  • 22
  • 42
  • 1
    My application is calculating several numbers. When you make application inactive all the session specific variables are reinitialized and hence loose data. Any idea. – Namit May 23 '14 at 13:47
1

I had come across a similar issue, resolved this by overriding onPause() and onResume() methods, save all the required variables in onPause() and restore these in onResume().

@Override
protected void onResume(){
    super.onResume();
    load();
}

@Override
protected void onPause(){
    super.onPause();
    save();
}

private void save() {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("DeviceName", deviceName);
    editor.putString("ConnectOption", connectOption.toString());
    editor.commit();
}

private void load() { 
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    deviceName = sharedPreferences.getString("DeviceName","");
    String connectop = sharedPreferences.getString("ConnectOption","USB"); //You could provide a default value here

}