像这样的东西怎么样:
public interface DBRunnable {
public void executeDBTask();
public void postExecuteDBTask();
}
private class DBTask extends AsyncTask<DBRunnable, Void, DBRunnable> {
@Override
protected DBRunnable doInBackground(DBRunnable...runnables) {
runnables[0].executeDBTask();
return runnables[0];
}
@Override
protected void onPostExecute(DBRunnable runnable) {
runnable.postExecuteDBTask();
}
}
这将被称为:
new DBTask().execute(new DBRunnable() {
@Override
public void executeDBTask() {
// execute your db code here
}
@Override
public void postExecuteDBTask() {
// run your post execute code here
}
});
DBRunnables 可以保存在一个地方,以使代码更易于阅读和维护。