将参数传递给Asynctask

时间:2011-09-03 17:02:07

标签: android android-asynctask

我正在使用异步任务从菜单活动中获取字符串并加载一些东西..但是我 不能这样做..我以正确的方式使用它,我正确地传递参数? 请参阅代码段。感谢

  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
    }
}

4 个答案:

答案 0 :(得分:28)

避免添加构造函数。 只需在任务执行方法

中传递参数即可
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

}

答案 1 :(得分:15)

而不是我会做

 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
    }
}

并在调用asynctask之前检查“identifier”以防止创建AsyncTask的开销

像这样

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

答案 2 :(得分:4)

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

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

答案 3 :(得分:0)

AsyncTask意味着doInBackground()返回Void,onProgressUpdate()接受Integer参数和doInbackground接受... String params!

所以你不需要(并且真的不应该)使用Intent,因为它意味着用于通过Activities而不是Threads传递参数。

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

public class Setup...
{
    private String identifier;

    public Setup(String a) {
    identifier = a;
    }
}

希望它可以提供帮助。 此致

相关问题