静态方法中的 AsyncTask - 良好的编码实践?
我目前有一个辅助类来执行基本的异步任务,如下所示。我在需要时从活动中调用该函数。该代码似乎工作正常,我没有遇到任何问题。但是,我想知道这是否是一种良好的编码实践,或者是否有任何我不知道的后果。任何反馈都将很乐意接受和赞赏。
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为这样做有什么问题。作为静态函数,您不会隐藏稍后可能会困扰您的隐式
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.