0

サーバーに新しいコンテンツがあるかどうかを確認するスプラッシュ画面があります。showの場合、AlertDialogをアプリのユーザーに表示して、ユーザーのアクションに応じて、つまり、YESの場合はサーバーから新しいコンテンツをダウンロードし、NOの場合はサーバーからアプリのコンテンツをロードしてデータベースに取得します。

ただし、AsyncTask内でアラートダイアログを使用できません。私のスニペットコードは次のとおりです。

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

2 に答える 2

5

これは、メソッドdoInBackgroundが非UIスレッドで実行AlertDialogされ、UIスレッドに表示する必要があるためです。

問題を解決するには、のコードをAlertDialogのメソッドonProgressUpdateに移動します。次に、からの呼び出しAsyncTaskを表示する場合は、DialogpublishProgress()doInBackground

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 )

于 2011-11-10T08:11:17.147 に答える
3

UIスレッドとは、UIに直接関連付けられていることを意味します。ネットワークデータフェッチ、ディスクアクセスなど、UIスレッドで長い処理時間を必要とする操作を実行することはできません。それらは別のスレッドで実行する必要があります。

AsyncTask別々のスレッドでこれらの操作を実行するのに役立ちます。そうしないと、悪名高いANRエラーが発生する可能性があります(アプリケーションが応答しません)。

AsyncTaskには、onPreExecute() onPostExecute() onProgressUpdate() ,doInBackground ()UIスレッドにアクセスできるなどのメソッドが含まれていonPreExecute() onPostExecute() onProgressUpdate()ます。より長い処理時間を必要とする操作は、で実行する必要があります。doinbackground().

質問の機能要求がわかりませんが、データフェッチ操作Alert Dialog 前に表示したい場合は、onPreExecute()から表示するか、データフェッチ に表示したい場合は、onPostExecute()から実行してください。

于 2011-11-10T09:19:52.537 に答える