简体   繁体   English

Android应用程序因来电而被杀死

[英]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. 我也想跟踪光标,以便当再次打开应用程序时,它将从停止并完成作业的完全相同的位置继续。

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);
       }
      }
     };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM