静态方法中的AsyncTask-良好的编码习惯? [英] AsyncTask inside a Static method - Good Coding Practice?

查看:238
本文介绍了静态方法中的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.

这篇关于静态方法中的AsyncTask-良好的编码习惯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆