AsyncTask 的实例
我有一个程序,每次单击按钮时都会使用异步任务...我不想每次单击时都输入整个 AsyncTask ..那会很乏味。有什么更好的方法可以做到这一点? 这是一些源代码。
new AsyncTask<Void, Integer, Void>(){
@Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
text1.setText("Nice to meet you "+name);
dismissDialog(typeBar);
}
@Override
protected void onPreExecute() {
typeBar = 0;
showDialog(typeBar);
}
}.execute((Void)null);
}
});
}
I have a program that used async task everytime the button is clicked... I dont want to keep typing the WHOLE AsyncTask everytime it is clicked.. That will be to tedious. What is a better way i can do this?
Here is some source code.
new AsyncTask<Void, Integer, Void>(){
@Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
text1.setText("Nice to meet you "+name);
dismissDialog(typeBar);
}
@Override
protected void onPreExecute() {
typeBar = 0;
showDialog(typeBar);
}
}.execute((Void)null);
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个扩展 AsyncTask 的新类:
然后无论您在哪里需要它,只需执行以下操作:
就是这样!玩得开心!
Create a new class that extends AsyncTask:
Then whereever you need it just do this:
Thats it! Have fun!
将其放在公共或私人班级中。然后,您可以根据新创建的类的名称引用/实例化它。
Put it in a public or private class. You can then reference/instantiate it based on the name of the newly made class.