Android 解除屏幕锁定的警报对话框

Android 解除屏幕锁定的警报对话框,android,Android,当屏幕被锁定和解锁时,我的警报对话框将关闭。在我的情况下 1) 在asynctask(这是内部类)中启动连接时,这里的进度对话框开始显示“请稍候…”。 2) 连接完成后,ProgressDilog被解除,并显示警报消息 alertBuilder = new AlertDialog.Builder(Registration.this); alertBuilder.setMessage(Constants.TEXT_REGISTERED_SUCCESSFULLY); alertBuilder.set

当屏幕被锁定和解锁时,我的警报对话框将关闭。在我的情况下

1) 在asynctask(这是内部类)中启动连接时,这里的进度对话框开始显示“请稍候…”。 2) 连接完成后,ProgressDilog被解除,并显示警报消息

alertBuilder = new AlertDialog.Builder(Registration.this);
alertBuilder.setMessage(Constants.TEXT_REGISTERED_SUCCESSFULLY);
alertBuilder.setCancelable(false);
alertBuilder.setPositiveButton(Constants.TEXT_OK, new android.content.DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
    Util.saveData(Registration.this);
    Intent thanksYouIntent = new Intent(Registration.this,ThankYouActivity.class);
    Registration.this.finish();
} });
alertBuilder.create().show();
因此,在此过程中,当我在连接启动时锁定屏幕时,不会显示警报消息,并且会显示之前状态下的相同活动

alertBuilder = new AlertDialog.Builder(Registration.this);
alertBuilder.setMessage(Constants.TEXT_REGISTERED_SUCCESSFULLY);
alertBuilder.setCancelable(false);
alertBuilder.setPositiveButton(Constants.TEXT_OK, new android.content.DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
    Util.saveData(Registration.this);
    Intent thanksYouIntent = new Intent(Registration.this,ThankYouActivity.class);
    Registration.this.finish();
} });
alertBuilder.create().show();
这是我启动对话框的代码。我听说要将对话框绑定到活动,所以我尝试使用alertBuilder.create().setOwnerActivity(RegistrationActivity.This)。这也没有显示任何结果

我不清楚的一件事是,当父活动暂停时,运行连接的内部asyncTask会发生什么情况。任何主体都可以帮助我

提前感谢,, Sha。

用于显示对话框的覆盖。这会有帮助。

@sha
在屏幕关闭和打开期间会引发一些事件,您也需要控制这些事件。。。。如果我错了,链接中会列出一些与此相关的示例。根据我的观察,当活动处于暂停状态时,相应的异步任务将停止。 所以,在我的例子中,当屏幕被锁定时,位于asynctask的doInBackground中的连接已经开始执行。但是由于屏幕锁定的原因,asynctask停止,onPostExecute没有成功完成。onPostExecute中的alertDialog没有显示。所以,我正在保存连接的响应状态,并在屏幕上使用布尔检查调用oncreate解锁时显示它

下面是代码。这是一个巨大的数字,但无法解释我的情况


   private static boolean alertDialogDismissedUnExpectedly;
private static String _alertMessage; //alert Message is for saving the previous alert message displaying when screen is locked

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //coding part

  _registerImageView.setOnClickListener(new OnClickListener() {//here the asynctask starts running on click
    private String _response;
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        _alertMessage = "";
        //post data for response
        String postcontent="Sucess";
        if(Constants.LOG)Log.d("Content to post is :", ""+postcontent);
        ResgiserAsyncTask asyncTask = new ResgiserAsyncTask(postcontent);
        asyncTask.execute(null);


   }
  if(alertDialogDismissedUnExpectedly && savedInstanceState != null){ // check for Alert Message dismissed unexpectedly

    if(_alertMessage == null ? true : _alertMessage.equalsIgnoreCase("")){ //intialise the last state of alert message if no alert message is set

        _alertMessage = _Engine.get_returnMessage();//this is my engine where parsing is done,So i'll get the previous response of connection

    }

    if(_alertMessage != null){
        if(_alertMessage.equalsIgnoreCase(Constants.TEXT_REGISTERED_SUCCESSFULLY) || _alertMessage.equalsIgnoreCase(Constants.TEXT_SUCCESS)){//my success case

            raiseSuccessDialog();//this is internal method

        }else{//failure case

            raiseDialog(_alertMessage);//this is internal method

        }
    }

  }else{

    alertDialogDismissedUnExpectedly = false;
    _alertMessage = "";
  }
}

private class ResgiserAsyncTask extends AsyncTask{
    private String _postContent;
    private Document _document;
    public ResgiserAsyncTask(String postContent) {
        // TODO Auto-generated constructor stub
        alertDialogDismissedUnExpectedly = true;//set the coolean to true and make it false Clicklistener of alertDialog
        _postContent= postContent;
    }
    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        _document = Util.postPage(Constants.URL_REGISTRATION, _postContent, true);
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if(_document != null){

            String _response = _Engine.parseRegistration(_document);
            if(!Constants.TEXT_SUCCESS.equalsIgnoreCase(_response)){
                raiseDialog(_response);
            }else{

                raiseSuccessDialog();
            }

        }
    }

}

private void raiseSuccessDialog(){

        _alertMessage = Constants.TEXT_REGISTERED_SUCCESSFULLY;
        alertBuilder = new AlertDialog.Builder(Registration.this);
        alertBuilder.setMessage(Constants.TEXT_REGISTERED_SUCCESSFULLY);
        alertBuilder.setCancelable(false);
        alertBuilder.setPositiveButton(Constants.TEXT_OK, new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                alertDialogDismissedUnExpectedly = false;
                Intent thanksYouIntent = new Intent(Registration.this,ThankYouActivity.class);
                startActivity(thanksYouIntent);
            }
        });
        alertBuilder.create().show();

}

private void raiseDialog(String message) {
        // TODO Auto-generated method stub
    _alertMessage  = message; // intialise the message to the raised dialog so that when screen is locked the alert can be displayed once again
    AlertDialog.Builder alertBuilder = new Builder(Registration.this);
        alertBuilder.setMessage(message);
        alertBuilder.setCancelable(false);
        alertBuilder.setPositiveButton(Constants.TEXT_OK, new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                alertDialogDismissedUnExpectedly = false;
            }
        });
        alertBuilder.create().show();

}

希望它对那些面临同样问题的人有所帮助。请提供更好的建议。

您是否用一些LogCat消息覆盖了onPause和onDestroy,以查看屏幕锁定时应用程序发生了什么情况?是的,我已经覆盖了它们。我还覆盖了onsavedinstace方法并保存了当前活动包。当屏幕锁定并unlocked oncreate方法再次调用。感谢您的响应。在oncreate方法中,我们将创建对话框并返回它。我的疑问是,如果我们仅从“onCreateDialog()返回对话框,android活动如何将对话框绑定到它“?我实现了这个东西。但没有按预期工作。谢谢你的回复。这是一篇好文章。我通过使用一些布尔值并检查互联网连接是否完成来解决我的问题。更新:清单中的单个配置修复了安卓系统的问题:configChanges=“keyboardHidden | orientation”