我正在使用异步任务从菜单活动中获取字符串并加载一些东西..但我是
无法这样做..我是否以正确的方式使用它并且我是否正确传递参数?
请查看代码片段。谢谢
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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)