我正在阅读AsyncTask,我尝试了下面的简单程序.但它似乎没有用.我怎样才能使它工作?
public class AsyncTaskActivity extends Activity {
Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener((OnClickListener) this);
}
public void onClick(View view){
new LongOperation().execute("");
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
for(int i=0;i<5;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
TextView txt = (TextView) findViewById(R.id.output); …Run Code Online (Sandbox Code Playgroud) 如果不将它放在子类的构造函数中,编译器是否会自动放置这个?
那意味着我甚至不需要关心它?在一些文章中,他们把它说出来.
如果我有一个带参数的构造函数,这将是构造函数,还是它需要一个没有参数列表的构造函数?
我想AsyncTask在我的应用程序中使用一个,但我无法找到一个代码片段,其中简单解释了工作原理.我只想要一些东西来帮助我快速恢复速度,而不必再次浏览文档或大量的问答.
我需要将两个参数(lv和位置)传递给Asynctask doInBackground方法,但我不知道它是怎么做的......
LoadSound.execute(lv,position)
class LoadSound extends AsyncTask<**[ListView,int]**, String, String>
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑:
我有一个listView.如果您单击项目播放声音(来自Internet).
我想用Asynctask显示progressDialog.
在doInBackground方法中,我有itemOnClick:
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Run Code Online (Sandbox Code Playgroud)
因此我需要传递lv和位置.