当后退按钮点击两次时关闭整个应用程序?

时间:2018-04-21 00:07:57

标签: java android android-studio

我正在使用此代码:

 private static long back_pressed;

    @Override
    public void onBackPressed() {

        if (back_pressed + 2000 > System.currentTimeMillis()) super.onBackPressed();
        else Toast.makeText(getBaseContext(), "click again to leave", Toast.LENGTH_SHORT).show();
        back_pressed = System.currentTimeMillis();
      }

它只关闭当前的Activity,如何在单击两次后退按钮时关闭整个应用程序?

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

@Override
 public void onBackPressed() {

    if (back_pressed + 2000 > System.currentTimeMillis()) {

     android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(1);
    }
    else Toast.makeText(getBaseContext(), "click again to leave", Toast.LENGTH_SHORT).show();
    back_pressed = System.currentTimeMillis();
 }