Android的 - 确认应用程序退出烤面包应用程序、烤面包、Android

2023-09-05 11:22:30 作者:、罙薆沵.、私耐沵.

我是新的Andr​​oid开发,我想它,以便当用户presses的主要活动的返回按钮,会出现一个确认退出由$ P $再次pssing返回按钮消息举杯消息。我将如何做到这一点?这是我到目前为止有:

  @覆盖
公共无效onBack pressed(){
    // TODO自动生成方法存根
    super.onBack pressed();
    吐司S = Toast.makeText(getBaseContext(),回来了preSS退出,Toast.LENGTH_LONG);
    s.show();
    等待();

    公共布尔onBack pressed(){
        完();
    }
}
 

解决方案

我只想救背面preSS的时间,然后最新preSS的时间比较新的preSS。

 终于preSS;
@覆盖
公共无效onBack pressed(){
    长currentTime的= System.currentTimeMillis的();
    如果(currentTime的 - 最后preSS> 5000){
        Toast.makeText(getBaseContext(),回来了preSS退出,Toast.LENGTH_LONG).show();
        最后preSS = currentTime的;
    }其他{
        super.onBack pressed();
    }
}
 

打开任何电脑程序都会出现应用程序无法正常启动 0xc0000005 .请单击 确定 关闭应用程序.

I'm new to Android development and I want it so when the user presses the back button on the main activity, a toast message appears with a "confirm exit by pressing the back button again" message. How would I do this? This is what I have so far:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    Toast s = Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG);
    s.show();
    wait();

    public boolean onBackPressed() {
        finish();    
    }
}

解决方案

I would just save the time of the backpress and then compare the time of the latest press to the new press.

long lastPress;
@Override
public void onBackPressed() {
    long currentTime = System.currentTimeMillis();
    if(currentTime - lastPress > 5000){
        Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG).show();
        lastPress = currentTime;
    }else{
        super.onBackPressed();
    }
}