如何解决在ScreenLock上关闭“警报”对话框
| 屏幕锁定和解锁后,我的警报对话框消失了。 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.create()。setOwnerActivity(RegistrationActivity.this)。这也没有显示任何结果。
我不清楚的一件事是当父活动暂停时正在运行Connection的内部asyncTask会发生什么情况。任何主体请帮助我。
提前致谢,
沙。
解决方法
如果错误,请纠正我。当我的观察到一个活动处于暂停状态时,相应的AsyncTask将停止。
因此,在我的情况下,当屏幕锁定时,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,int which) {
// TODO Auto-generated method stub
alertDialogDismissedUnExpectedly = false;
}
});
alertBuilder.create().show();
}
希望它对遇到同样问题的人有所帮助。
, 覆盖onCreateDialog()
以显示对话框。它会有所帮助。
, @sha
屏幕关闭期间会引发一些事件,您也需要对其进行控制。...链接http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-意图/鞋子有关此的一些示例
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。