gpt4 book ai didi

android - 屏幕锁定时关闭警告对话框

转载 作者:行者123 更新时间:2023-11-29 22:25:01 25 4
gpt4 key购买 nike

当屏幕锁定和解锁时,我的警告对话框消失。在我的例子中,

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

所以,在这个过程中,当我在连接启动时锁定屏幕时,不会显示警告消息,而是显示之前状态下的相同 Activity 。

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();

这是我提出对话框的代码。我听说要将对话框绑定(bind)到 Activity ,所以我尝试了 alertBuilder.create().setOwnerActivity(RegistrationActivity.this)。这也没有显示任何内容结果。

我不清楚的一件事是当父 Activity 暂停时正在运行 Connection 的内部 asyncTask 会发生什么。请任何人帮助我。

提前致谢,沙。

最佳答案

如果我错了请纠正我。根据我的观察,当 Activity 处于暂停状态时,相应的 AsyncTask 将停止。所以,在我的情况下,当屏幕被锁定时,asynctask 的 doInBackground 中的连接已经开始执行。但是由于屏幕锁定原因,asyncTask 已停止并且 onPostExecute 未成功完成。我在 onpostexecute 中的 alertDialog 未显示。所以,我'保存连接的响应状态并在屏幕上调用 oncreate 时显示它使用 bool 检查解锁。

下面是代码。代码量很大,但无法解释我的情况。


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();

}

希望对遇到同样问题的人有所帮助。欢迎提出更好的想法。

关于android - 屏幕锁定时关闭警告对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6186469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com