Alertdialog在屏幕锁定上解雇

时间:2011-05-31 10:52:15

标签: android

屏幕锁定和解锁时,我的警报对话框正在解除。在我的情况下,

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

因此,在此过程中,当我在启动Connection时锁定屏幕时,不显示警报消息,并显示之前状态的相同活动。

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发生了什么,当父活动暂停时运行Connection.Any body plz帮助我。

提前致谢, 沙。

3 个答案:

答案 0 :(得分:2)

如果错误,请纠正我。当活动处于暂停状态时,我的观察结果,相应的AsyncTask停止。 所以,在我的情况下,当屏幕被锁定连接时,asInBackground中的asynctask已经开始执行。但是由于屏幕锁定的原因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();

}

希望对那些遇到同样问题的人有所帮助。邀请任何更好的想法。

答案 1 :(得分:0)

覆盖onCreateDialog()以显示对话框。它可以帮助。

答案 2 :(得分:0)

@sha  屏幕关闭期间会有一些事件发生,你需要控制它....链接http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/有关这方面的一些例子