2011-05-31 105 views
1
當屏幕被鎖定

我的警告對話框被解僱和Unlocked.In我的情況下,警告對話框駁回對屏幕鎖

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

這是我提高dialog.I代碼聽到對話的活動結合,所以我試圖像alertBuilder.create()。setOwnerActivity(RegistrationActivity.this)。這也沒有表現出任何結果。

有一件事我沒有清楚的是,當父活動暫停時正在運行Connection的內部asyncTask會發生什麼情況。任何身體PLZ都會幫助我。

在此先感謝, 沙。

+0

您是否覆蓋onPause和onDestroy以及一些LogCat消息,以查看屏幕鎖定時發生了什麼? – Blundell 2011-05-31 11:01:09

+0

是的,我已經覆蓋了他們。我也覆蓋了onsavedinstace方法,並保存當前活動的捆綁。當屏幕鎖定和解鎖oncreate方法再次調用。 – sha 2011-05-31 16:18:53

回答

2

一些例子糾正我,如果上午wrong.Form我的觀察,當一個活動處於暫停狀態,相應的AsyncTask停止。 所以,在我的情況下,當屏幕被鎖定連接,這是在asynctask的doInBackground已經開始執行。但由於屏幕鎖定原因asyncTask停止和onPostExecute不成功完成。我的alertDialog onpostexecute不顯示。所以,我保存連接的響應狀態並在屏幕上調用oncreate時顯示它,使用布爾檢查進行解鎖。

下面是code.This是巨大的,但不能削減更多的解釋我的情況。


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

} 

希望它有助於那些面臨同樣問題的人。邀請所有更好的想法。

+0

**更新**:清單中的單個配置修復了問題 android:configChanges =「keyboardHidden | orientation」 – sha 2011-10-10 06:19:18

0

覆蓋onCreateDialog()用於顯示對話框。它可以幫助。

+0

感謝您的迴應。在創建方法上,我們將創建對話框並將其返回。我疑惑的是,如果我們只是從'onCreateDialog()'返回一個對話框,那麼android活動如何將對話框綁定到它呢?我實現了這個東西。按預期工作。 – sha 2011-05-31 16:21:26