Android AsyncTask Common
AsyncTaskは時間がかかる作業(HTTP通信、データベースクエリ、BT通信等)をBackgroundで処理する時に よく使われますが、Activity毎に作成したりするのは面倒くさいので、まとめてみました。・BaseTask作成
バックグラウンド処理中、ProgressDialogを表示する
package com.park.commonasynctask.common; import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import java.lang.ref.WeakReference; /** * Filename : BaseTask.java * Function : * Comment : バックグラウンド処理の間にキャンセルできないプログレスダイアログを表示する * * History : 2016/03/31, su min park, develop * * @version 1.0 * @author su min park * @since JDK 1.7 */ public abstract class BaseTask<Object, Void, T> extends AsyncTask<Object, Void, T> { private final Context mContext; private final WeakReference<ProgressDialog> mProgressRef; public BaseTask(Context context, int rMessage) { mContext = context; ProgressDialog dialog = new ProgressDialog(context); dialog.setMessage(mContext.getString(rMessage)); dialog.setCancelable(false); mProgressRef = new WeakReference<>(dialog); } @Override protected void onPreExecute() { super.onPreExecute(); ProgressDialog dialog = mProgressRef.get(); if (dialog != null && !dialog.isShowing()) { dialog.show(); } } @Override protected void onPostExecute(T result) { super.onPostExecute(result); ProgressDialog dialog = mProgressRef.get(); if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } } @Override protected void onCancelled() { super.onCancelled(); ProgressDialog dialog = mProgressRef.get(); if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } } }
・実装クラス作成
今回はデータベースへのクエリを想定して作成しました。
QueryExecutorCallbackを利用して、処理結果を戻すようになっています。
package com.park.commonasynctask.database; import android.content.Context; import com.park.commonasynctask.R; import com.park.commonasynctask.common.BaseTask; import com.park.commonasynctask.constants.Globals; import com.park.commonasynctask.database.connection.DatabaseHelper; import com.park.commonasynctask.exception.InitializeException; import com.park.commonasynctask.util.LogUtil; /** * Filename : QueryExecutor.java * Function : * Comment : 非同期でQueryを実行する * * History : 2016/03/31, su min park, develop * * @version 1.0 * @author su min park * @since JDK 1.7 */ public class QueryExecutor<T> extends BaseTask<Object, Void, T> { public enum WhichDatabase { MASTER } private Context mContext; private Query mQuery; private QueryExecutorCallback mCallback; private DatabaseHelper mDatabaseHelper; private WhichDatabase mWhichService; private Exception e = null; public QueryExecutor(Context context, WhichDatabase whichService, QueryExecutorCallback callback) { this(context, whichService, callback, R.string.common_loading); } public QueryExecutor(Context context, WhichDatabase whichService, QueryExecutorCallback callback, int message) { super(context, message); mContext = context; mWhichService = whichService; mCallback = callback; } public QueryExecutor setQuery(Query query) { this.mQuery = query; return this; } @Override protected void onPreExecute() { super.onPreExecute(); LogUtil.d("before "); try { switch (mWhichService) { case MASTER: mDatabaseHelper = new DatabaseHelper(mContext, Globals.getInstance().DB_MASTER_NAME, Globals.getInstance().DB_MASTER_VERSION); break; } } catch (InitializeException e1) { LogUtil.e(e1); } } @Override protected T doInBackground(Object... params) { try { if (mQuery.getType() == Query.TYPE.SELECT) { LogUtil.d("run query--- "); Thread.sleep(3000); } else if (mQuery.getType() == Query.TYPE.DELETE) { // DELETE EXECUTOR CODE } else if (mQuery.getType() == Query.TYPE.INSERT) { // INSERT EXECUTOR CODE } } catch (Exception e) { this.e = e; } return null; } @Override protected void onPostExecute(T result) { super.onPostExecute(result); LogUtil.d("after"); if (e == null) { if (mCallback != null) { mCallback.returnResult(result); } if (mDatabaseHelper != null) { mDatabaseHelper.close(); mDatabaseHelper = null; } } else { // Something went wrong. // Do something with exception on e.printStackTrace(); } } public static class Query { public enum TYPE { SELECT, DELETE, INSERT } String column; String orderBy; String tableFrom; String where; String[] bindWhere; boolean whereIn; TYPE type; public Query(TYPE type) { this.type = type; } public Query setSelectColumn(String column) { this.column = column; return this; } public Query setOrderByColumn(String orderBy) { this.orderBy = orderBy; return this; } public Query setSelectTable(String tableName) { this.tableFrom = tableName; return this; } public Query setSelectWhere(String where) { this.where = where; return this; } public Query setBindWhere(String... args) { bindWhere = args; return this; } public Query setWhereIn(boolean whereIn) { this.whereIn = whereIn; return this; } public TYPE getType() { return type; } } public interface QueryExecutorCallback<T> { void returnResult(T result); } }
・Activityでの呼び出し
QueryExecutor.Query query = new QueryExecutor.Query(QueryExecutor.Query.TYPE.SELECT); new QueryExecutor(MainActivity.this, QueryExecutor.WhichDatabase.MASTER, new QueryExecutor.QueryExecutorCallback<Map<String, String>>() { @Override public void returnResult(Map<String, String> result) { LogUtil.d("result"); } }).setQuery(query).execute();
詳細のソースコードはGithubをご参照ください。
https://github.com/kongbab04/CommonAsyncTask
0 コメント:
コメントを投稿