15

AsyncTaskクラスに関連する概念上の問題があります。AsyncTaskメインUIがブロックされないように使用します。しかし、デバイスのメモリからデータを取得したいとします。これにはAsyncTaskクラスを使用します。コードの関連する行は次のようになります(返されるデータ型が文字列であると想定)。

  //code
    String data = new ExtendedAsyncTask().execute(param1, param2).get();
  //use this returned value.

上記の行はUIをブロックし、?を使用する目的を無効にしAsyncTaskませんか?はいの場合、UIをブロックせずに関連データを取得するにはどうすればよいですか?次のコード行では、タスクを実行するためにこのデータが必要になるため、戻り値によって異なります。

ありがとう

4

3 に答える 3

20

get() method will block the UI thread。関連データを取得するには、doInBackgroundから値を返し、onPostExecuteパラメーターで値をキャプチャする必要があります。

doInBackgroundによって返される値は、onPostExecuteメソッドによってキャプチャされます

例:

public class BackgroundTask extends AsyncTask<String, Integer, String >{
       private ProgressDialog mProgressDialog;
       int progress;
       public BackgroundTask() {
           mProgressDialog = new ProgressDialog(context);
             mProgressDialog.setMax(100);
             mProgressDialog.setProgress(0);
    }

       @Override
    protected void onPreExecute() {
           mProgressDialog =ProgressDialog.show(context, "", "Loading...",true,false);
        super.onPreExecute();
    }
     @Override
     protected void onProgressUpdate(Integer... values) {
     setProgress(values[0]);
  }

    @Override
    protected String doInBackground(String... params) {
            String data=getDatafromMemoryCard();    

        return data;  // return data you want to use here
    }
    @Override
    protected void onPostExecute(String  result) {  // result is data returned by doInBackground
        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
        mProgressDialog.dismiss();
        super.onPostExecute(result);
    }
   }

別のクラスでasynctaskを使用している場合は、次のようなコールバックインターフェイスでAsyncTaskを使用します

これは、コールバックを使用した同じAsyncTaskについて以前に提供した回答です。

于 2013-03-26T11:06:38.860 に答える
1

非同期タスクが実行されると、タスクは次の4つのステップを実行します。

1.onPreExecute(), invoked on the UI thread before the task is executed. use this to diaply progress dialog.

2.doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. Can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

3.onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). Used to publish progress.

4.onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

アクティビティonCreate()

    TextView tv;
   @Override
   protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);
    tv= (TextView)findViewById(R.id.textView);
    new TheTask().execute();
      
  }
class TheTask extends AsyncTask<Void, Void,String> {

  protected void onPreExecute() {
  //dispaly progress dialog
 }

 protected String doInBackground(Void... params) {
   //do network operation
     return "hello"; 
 }

 protected void onPostExecute(String result) {  
  //dismiss dialog. //set hello to textview
      //use the returned value here.
     tv.setText(result.toString());
 }
 }

非同期呼び出しを行い、UIスレッドで通知します。

更新:AsyncTaskは非推奨になりました。kotlinコルーチンに切り替えます。https://developer.android.com/kotlin/coroutines

于 2013-03-26T11:11:53.863 に答える
0

この方法では結果が得られません。例については、次のリンクを参照してください:https ://github.com/levinotik/ReusableAsyncTask/tree/master/src/com/example

基本的に、これがあなたがする必要があることです:

  • アクティビティが実装するインターフェース(=リスナー)を定義します
  • asynctaskでリスナーを設定します
  • onPostExecuteでyourListener.yourMethod()を呼び出します
于 2013-03-26T11:09:28.677 に答える