【问题标题】:Android AsyncTask Dialog AlertAndroid AsyncTask 对话框警报
【发布时间】:2011-12-25 23:03:41
【问题描述】:

如果服务器上有任何新内容,我有一个启动屏幕,它会检查 URL。如果显示我希望向应用程序用户显示 AlertDialog,以便根据用户的操作,即如果是,则从服务器下载新内容并将其获取到数据库,否则如果不从服务器加载应用程序的内容。

但是我无法在 AsyncTask 中使用警报对话框 我的 sn-p 代码如下:

protected String doInBackground(String... mode){
  /* I AM QUERY MY DATABASE FOR THE PREVIOUS CONTENT(SAY CONTENT 1, CONTENT 2);
  * then i start my connection to the server which has an xml file with the content
  * info (say content 3), at this stage .
  * i Check certain condition here,
  * say if result ==0 then i wish to display an alertdialog.
  * I used an alert dialog and i get some error. Stating use Looper or Handler.

  * Can anyone help me with this?
  */  
}

已编辑

所以在

doInBackGround(String... mode){
  if(result==0){
    // how do i implement this alert show that if the dialog appears and on clicking Yes i wish to exectute the URL handling part below        

    AlertDialog.Builder alert = new AlertDialog.Builder(context);
    alert.setTitle("Updates Found");
    alert.setMessage( "New Updates for has been found.\n Would you like to download ?\n"
              + "Whats the update: Checking "+pld.result.get(i).get( "issue" ));
    alert.setIcon(android.R.drawable.ic_dialog_info);                              
    alert.setPositiveButton(android.R.string.yes,
      new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
          try { 
            URL url = new URL(pld.result.get(i).get("link"));
            ManageZipFile.getArchive(url,pld.result.get(i).get("issue"), file); 
          } 
          catch (Exception ex) { 
            Log.d(TAG, "Exception from URL"+ ex.getMessage()); 
          }

          progressState += updateProgressBar(20);
          pld.saveCoverData(pld.result.get(i).get("issue"));
          try { 
            pldContent = new PullLoadData(getString(R.string.files_path) 
                                          + pld.result.get(i).get("issue") 
                                          + "/contents.xml",context); 
            pldContent.getNewsItems();
            progressState += updateProgressBar(20); 
          } 
          catch(XmlPullParserException e) { 
            Log.e(TAG, "GetNEWSITEM "+ e.getMessage()); 
          } 
          catch (IOException e) { 
            Log.e(TAG, "XML FIle not found" + e.getMessage()); 
          } 
        } 
     });

     alert.setNegativeButton(android.R.string.no, 
       new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int arg1) { 
           dialog.dismiss();
       }
     }); 

     AlertDialog showAlert = alert.create();
     showAlert.show();
   }
 }

【问题讨论】:

    标签: android exception android-asynctask looper


    【解决方案1】:

    这是因为doInBackground方法在非UI线程上运行,而AlertDialog需要在UI线程上显示。

    要解决您的问题,请将AlertDialog 的代码移动到AsyncTask 中的onProgressUpdate 方法,然后当您要显示Dialog 时,请从doInBackground 调用publishProgress()

    protected String doInBackground( String... mode ) {
      if( something )
        //Conditions for showing a Dialog has been met
    }
    
    protected void onProgressUpdate( Void... params ) {
      //Show your dialog.
    }
    

    如果您需要将某种变量/数据传递给对话框,您可以传递您在extends AsyncTask<Params, Progress, Result> 中声明的数据类型 - 其中Progress 是您可以通过publishProgress( myVariable ) 传递的参数类型-方法。

    【讨论】:

    • 我编辑了我的问题,请您详细说明一下。我对线程,事情感到困惑
    • 你能帮我编辑一下吗? onPreExecute() 我创建了一个对话框,但是对话框有两个按钮 ok 并在我单击 ok 时取消,我需要做其他事情,例如 url 连接。
    【解决方案2】:

    UI 线程意味着它直接与您的 UI 相关联。您不能在您的 UI 线程中执行需要大量处理时间的操作,例如 网络数据获取、磁盘访问等。它们应该在单独的线程中完成。

    AsyncTask 有助于在单独的线程中执行这些操作,否则可能会导致臭名昭著的 ANR 错误。(应用程序无响应)。

    AsyncTask 包含onPreExecute() onPostExecute() onProgressUpdate() ,doInBackground ()etc 等方法,您可以从onPreExecute() onPostExecute() onProgressUpdate() 访问UI 线程。需要较长处理时间的操作应在doinbackground(). 中进行

    我无法理解您的问题的功能要求,但如果您想在 数据获取操作之前显示 Alert Dialog从 onPreExecute() 显示它 或者如果你想数据获取之后显示 从 onPostExecute() 执行。

    【讨论】:

    • 谢谢,我明白你说的和我读到的了吗?作为 inDobackGround 我无法启动我的 AlertDialog。你能偷偷溜进去吗
    • 当条件为真时,说 if(result==0){那么我只需要我在受保护的 void onPreExecute() 上创建的 AlertDialog 并且在我的对话框上我有两个按钮,OK CANCEL好的,我需要运行一个 URL CONNECTION}
    • 如果您的条件为真,则显示 alertdialog。如果响应正常,请执行 asynctask。这是您需要的吗?
    • developer.android.com/guide/topics/fundamentals/… 找到上面的链接并尝试阅读有关 asynctask ..
    • +1 获得不错的答案和 cmets!(不仅仅是因为您的显示名称):D :P
    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 2012-02-07
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多