【问题标题】:Passing parameters to Asynctask将参数传递给 Asynctask
【发布时间】:2011-09-03 17:02:07
【问题描述】:

我正在使用异步任务从菜单活动中获取字符串并加载一些东西..但我是 不能这样做..我是否以正确的方式使用它并且我是否正确传递了参数? 请查看代码 sn-p。谢谢

  private class Setup extends AsyncTask<Void, Integer, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            if (!(getIntent().getExtras().isEmpty())) {
                Bundle gotid = getIntent().getExtras();
                identifier = gotid.getString("key");
            }
        } catch (Exception e) {
            e.getStackTrace();
        } finally {

            if (identifier.matches("abc")) {
                publishProgress(0);
                db.insert_fri();
            } else if ((identifier.matches("xyz"))) {
                publishProgress(1);
                db.insert_met();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... i) {
        // start the song here
        if (i[0] == 0) {
            song.setLooping(true);
            song.start();
        }
    }

    @Override
    protected void onPostExecute(Void res) {

    }

    @Override
    protected void onPreExecute() {
        // do something before execution
    }
}

【问题讨论】:

    标签: android android-asynctask


    【解决方案1】:

    避免添加构造函数。

    只需在任务执行方法中传递您的参数

    new BackgroundTask().execute(a, b, c); // can have any number of params
    

    现在你的背景类应该是这样的

    public class BackgroundTask extends AsyncTask<String, Integer, Long> {
    
        @Override
        protected Long doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            String a = arg0[0];
            String b = arg0[1];
            String c = arg0[2];
            //Do the heavy task with a,b,c
            return null;
        }
        //you can keep other methods as well postExecute , preExecute, etc
    
    }
    

    【讨论】:

    • “避免添加构造函数”为什么?我试图确定为什么在异步任务中使用构造函数来初始化私有字段在 android 中是一件坏事。
    • 我并没有说这是一个坏主意,但只是为了传递参数不要通过构造函数路径,因为你最终会浪费内存。
    • 可能会浪费多少内存?
    【解决方案2】:

    我会这样做

     private class Setup extends AsyncTask<String, Integer, Void> {
    
        @Override
        protected Void doInBackground(String... params) {
        String identifier = params[0];
    
              if (identifier.matches("abc")) {
                    publishProgress(0);
                    db.insert_fri();
                } else if ((identifier.matches("xyz"))) {
                    publishProgress(1);
                    db.insert_met();
                }
            }
            return null;
        }
    
        @Override
        protected void onProgressUpdate(Integer... i) {
            // start the song here
            if (i[0] == 0) {
                song.setLooping(true);
                song.start();
            }
        }
    
        @Override
        protected void onPostExecute(Void res) {
    
        }
    
        @Override
        protected void onPreExecute() {
            // do something before execution
        }
    }
    

    并在调用异步任务之前检查“标识符”以防止创建异步任务的开销

    喜欢这个

    if (!(getIntent().getExtras().isEmpty())) {
                    Bundle gotid = getIntent().getExtras();
                    identifier = gotid.getString("key");
                   new Setup().execute(identifier);
        }
    

    【讨论】:

      【解决方案3】:

      一个简单的方法是添加一个构造函数:

      public Setup(String a, Int b) {
          this.a = a;
          this.b = b;
      }
      

      【讨论】:

        【解决方案4】:

        AsyncTask 表示 doInBackground() 返回 Void,onProgressUpdate() 采用 Integer 参数,doInbackground 采用... String 参数!

        所以你不需要(而且真的不应该)使用 Intent,因为它是用来通过活动而不是线程传递参数的。

        如前所述,您可以为您的类创建一个构造函数和一个名为“标识符”的全局参数

        public class Setup...
        {
            private String identifier;
        
            public Setup(String a) {
            identifier = a;
            }
        }
        

        希望它能有所帮助。 问候

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-07-29
          • 1970-01-01
          • 2011-05-10
          • 2011-03-05
          • 1970-01-01
          • 2012-05-09
          • 2013-07-13
          • 2012-08-17
          相关资源
          最近更新 更多