2016-12-27 9 views
0

現在、私は10個の独立した操作のために10個の別々のAsynctaskクラスを持っており、onPreExecute()とonProgressUpdate()すべてのAsynctaskクラスで同じです。これを単純化する他の方法はありますか?例えば、私は "ADD"という名前のユーザー定義関数を持っています。今のところ、10個のAsynctaskクラスすべてのonPreExecute()で "ADD"関数を呼び出しました。これを簡略化する方法は、interfaceまたはany-other elseonPreExecuteとonProgressUpdateをすべてのAsynctaskクラスに共通にする

答えて

3

AsyncTaskを拡張するBaseAsyncTaskである1つのクラスを作成します。 そして、onPreExecute()とonProgressUpdate()intの両方の実装をこのように記述します。

public abstract class BaseAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> { 

     @android.support.annotation.Nullable 
     private ProgressDialog progressDialog = null; 
     public Activity activity; 

     public BaseAsyncTask(Activity activity) { 
      this.activity = activity; 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      progressDialog = new ProgressDialog(activity, R.style.CustomProgressSpinner); 
      CommonUtilities.showDialog(progressDialog,activity); 
     } 

    @Override 
    protected void onProgressUpdate(Progress... values) { 
     super.onProgressUpdate(values); 
    } 

     @Override 
     protected void onPostExecute(Result result) { 
      super.onPostExecute(result); 
      CommonUtilities.dismissDialog(progressDialog); 
     } 
    } 

すべてのAsyncTaskでBaseAsyncTaskを拡張します。

public class AttachmentLoadTask extends BaseAsyncTask<DocumentVO, Void, File> {@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected File doInBackground(DocumentVO... documentVOs) { 
     File file = null; 
     return file; 
    } 

    @Override 
    protected void onProgressUpdate(Progress... values) { 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onPostExecute(File file) { 

     super.onPostExecute(file); 

    }} 
+0

あなたの答えははるかに優れています;)それはより良くできませんでした – beeb

+0

あなたのリプレイに感謝...その魅力のような作品.... –

0

onPreExecute()およびonProgressExecute()の操作で基本的な非同期タスクを作成します。次に、非同期タスククラスを作成します(基本非同期タスクから拡張します)。すべてAsynctaskが

だけで、その後にURIを作成するには、この

public interface MyAsyncInterface { 
    void processFinish(String output); 
} 

のようなインタフェースをクラス

public class MyConnectionClass extends AsyncTask<Uri, Void, Boolean> { 
    MyAsyncInterface delegate = null; 
    HttpURLConnection httpURLConnection; 
    String output; 

    public MyConnectionClass(MyAsyncInterface myAsyncInterface) { 
     delegate = myAsyncInterface; 
    } 

    @Override 
    protected void onPreExecute() { 

     super.onPreExecute(); 
    } 

    @Override 
    protected void onPostExecute(Boolean aBoolean) { 
     delegate.processFinish(output); 
     super.onPostExecute(aBoolean); 

    } 

    @Override 
    protected Boolean doInBackground(Uri... uris) { 
     try { 
      URL url = new URL(uris[0].toString()); 
      httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setDoOutput(true); 
      httpURLConnection.setDoInput(true); 
      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.connect(); 
      InputStream inputStream = httpURLConnection.getInputStream(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); 
      String line = null; 
      StringBuilder builder = new StringBuilder(); 
      while ((line = reader.readLine()) != null) { 
       builder.append(line); 

      } 
      output = builder.toString(); 
      return true; 


     } catch (MalformedURLException e) { 
      output = e.getMessage(); 
      return false; 

     } catch (IOException e) { 
      output = e.getMessage(); 
      return false; 

     } finally { 
      httpURLConnection.disconnect(); 
     } 

    } 
} 

を作成し、宣言呼び出すため

0

きっと、あなたは、単一のクラスを作成することがありますMyAsyncInterfaceを実装してから、onPostExecuteを呼び出してprocessFinishを呼び出し、呼び出し元のactiviにあるprocessFinish(String出力)メソッドに出力を渡しますty

+0

あなたのリプレイに感謝...その作品は魅力のようです.. .. –

関連する問題