将参数传递给Asynctask

Nik*_*hil 18 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
    }
}
Run Code Online (Sandbox Code Playgroud)

Him*_*der 30

避免添加构造函数.

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

new BackgroundTask().execute(a, b, c); // can have any number of params
Run Code Online (Sandbox Code Playgroud)

现在你的后台课应该是这样的

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

}
Run Code Online (Sandbox Code Playgroud)

  • "避免添加构造函数"为什么?我试图确定为什么使用构造函数来初始化asynctask中的私有字段在android中是一件坏事. (4认同)
  • 它可能浪费多少内存? (2认同)

Anu*_*ani 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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

像这样

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