gpt4 book ai didi

android - 将参数传递给 Asynctask

转载 作者:IT老高 更新时间:2023-10-28 23:17:53 26 4
gpt4 key购买 nike

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

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

最佳答案

避免添加构造函数。

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

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 - 将参数传递给 Asynctask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7294533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com