静态方法中的AsyncTask-良好的编码习惯?
我目前有一个帮助器类来执行基本的AsyncTasks,如下所示.我在需要时从活动中调用该函数.该代码似乎工作正常,我还没有遇到任何问题.但是,我想知道这是否是一种好的编码实践,还是我没有意识到任何分支.任何反馈都会很高兴被接受和赞赏.
I currently have a helper class to perform rudimentary AsyncTasks such as the following. I call the function from an activity as and when needed. The code seems to work fine and I haven't encountered any problems. But, I was wondering if this is a good coding practice or if there were any ramifications that I am unaware of. Any feedback would be gladly accepted and appreciated.
public class OtherUtils {
public static void updatePromptsOption(final boolean showPrompt, final Context context) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
Editor preferenceEditor = PreferenceManager.getDefaultSharedPreferences(context).edit();
preferenceEditor.putBoolean(Constants.SHOW_PROMPT, showPrompt).commit();
return null;
}
}.execute();
}
}
以这种方式做事我没有发现任何问题.作为静态函数,您不会隐藏隐式的this
引用,这些引用以后可能会给您带来麻烦.对我来说似乎是一个合理的便利功能.
I don't see anything wrong with doing things that way. Being a static function, you're not hiding implicit this
references that could bite you later. Seems like a reasonable convenience function to me.