Q:

Android application getting killed because of incoming call

In my application I am copying all the Call Logs into a Database in an Async Task. But If a call comes in between my application can get terminated leading to incomplete data. So first of all I want to stop my application from getting terminated in such scenario, so what should I do? Also I want to keep track of cursor such that when application is opened again it will continue from the exact same place where it stopped and complete the job.

A:

This is the code that helped me save my game state during incoming call.. Hope it helps you too...
    TelephonyManager tm;
    private PhoneStateListener mPhoneListener = new PhoneStateListener() {
      public void onCallStateChanged(int state, String incomingNumber) {
       try {
        switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
         Toast.makeText(GameActivity.this, "CALL_STATE_RINGING", Toast.LENGTH_SHORT).show();

         //Your function to save state right here...
         stopTimer();
         break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
         Toast.makeText(GameActivity.this, "CALL_STATE_OFFHOOK", Toast.LENGTH_SHORT).show();
         break;
        case TelephonyManager.CALL_STATE_IDLE:
         Toast.makeText(GameActivity.this, "CALL_STATE_IDLE", Toast.LENGTH_SHORT).show();
         break;
        default:
         Toast.makeText(GameActivity.this, "default", Toast.LENGTH_SHORT).show();
         Log.i("Default", "Unknown phone state=" + state);
        }
       } catch (Exception e) {
        Log.i("Exception", "PhoneStateListener() e = " + e);
       }
      }
     };

