关闭活动之前的Java Android显示消息(按回)

时间:2014-08-25 12:58:36

标签: java android

我试图在关闭应用程序之前(当用户按下后退消息时)在我的Android应用程序上显示消息。

到目前为止我所做的是:

public class MainActivity extends DroidGap  {

@JavascriptInterface
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();

    // new code
    super.setBooleanProperty("keepRunning", true);

    super.setIntegerProperty("splashscreen", R.drawable.splashscreen);
    super.setStringProperty("loadingDialog", "Loading Silver Angel ...");

    appView.addJavascriptInterface(this, "MainActivity");

    super.setIntegerProperty("loadUrlTimeoutValue", 10000); 
    super.loadUrl("file:///android_asset/www/index.html",10000);

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    //Handle the back button
    if(keyCode == KeyEvent.KEYCODE_BACK) {
        //Ask the user if they want to quit
        new AlertDialog.Builder(this)            
        .setTitle("Closing Silver Angel")
        .setMessage("Are you sure you want to close this application?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                //Stop the activity
                MainActivity.this.finish();    
            }

        })
        .setNegativeButton("No", null)
        .show();

        return true;
    }
    else {
        return super.onKeyDown(keyCode, event);
    }

}
}

我也尝试使用这种方法:

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
        .setTitle("Closing Activity")
        .setMessage("Are you sure you want to close this activity?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();    
        }

    })
    .setNegativeButton("No", null)
    .show();
}

我面临的问题是,当我在Nexus 7平板电脑上进行测试时,消息会显示1秒钟,并且随着应用程序自动关闭而消失。

由于

2 个答案:

答案 0 :(得分:2)

ya that's obvious as you have not told which button has been pressed use this code


@Override
    public void onBackPressed() {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                case DialogInterface.BUTTON_POSITIVE:
                    finish();
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(getResources().getString(R.string.do_you_really_want_to_exit)).setPositiveButton(getResources().getString(R.string.yes), dialogClickListener)
        .setNegativeButton(getResources().getString(R.string.no), dialogClickListener).show();
    }

Button_Positive的功能是yes在这里我关闭了活动而负面部分留空了所以不会发生任何事情

答案 1 :(得分:0)

您可以尝试Toast

Toast.makeText(YourActivity.this,"Good Bye, User!",Toast.LENGTH_SHORT).show();  

很多应用程序使用"按返回键两次退出"方法将消息显示为Toast

<强>更新
onBackPressed()中,显示一个提示是或否的对话框。如果用户按是,则调用super.onBackPressed() else,只需关闭对话框。