2017-09-19 73 views
0

我在3秒後安排了一次通話,如果該活動已關閉(用戶移至其他活動),我想取消該通話。
這是我的java文件:如何在活動結束後取消預定的呼叫?

public class EmargencyResponseActivity extends AppCompatActivity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_emergency_response); 


    if (ActivityCompat.checkSelfPermission(EmargencyResponseActivity.this, 
      Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
     ActivityCompat.requestPermissions(EmargencyResponseActivity.this,new String[]{Manifest.permission.CALL_PHONE},1); 


    } 
    new CountDownTimer(3000, 1000) { 
     public void onFinish() { 
      Intent startEmergency = new Intent(Intent.ACTION_CALL, Uri.parse("tel:108")); 
      startActivity(startEmergency); 
       finish(); 
      } 

     public void onTick(long millisUntilFinished) { 
     } 
    }.start();}} 
+0

請標記爲答案取消它作爲接受是否能解決你的問題 – SoroushA

回答

0

您可以在活動舉辦的計時器的引用,並在

public class EmargencyResponseActivity extends AppCompatActivity { 

CountDownTimer timer; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //... 

    } 
    timer = new CountDownTimer(3000, 1000) { 
     //Implementation 
    } 
} 

@Override 
protected void onPause(){ 
    if (timer != null) timer.cancel(); 
} 

}