Asynctask 输出 UI 线程
我想为 AsyncTask 类打上品牌,以便在后台执行一些操作,以进度更新调用者线程,但使用 UI 线程的调用者线程 != 。 我已经尝试过该代码,但publishProgress(i)行似乎没有效果。 有人可以告诉我如何修复它(或者我可以使用其他什么类)。 预先感谢 =)
public class MainUI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Thread t=new Thread(){
boolean exit=false;
public void run(){
Looper.prepare();
new DownloadFilesTask().execute();
while (!exit){
try {
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d("","Exit thread");
Looper.loop();
}
public void exit(){
exit=true;
}
class DownloadFilesTask extends AsyncTask<Void, Long, Long> {
protected Long doInBackground(Void... urls) {
long i=0;
for (i=0;i<20;i++){
Log.d("",i+" ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(i);
}
return i;
}
protected void onProgressUpdate(Long... progress) {
Log.d("Test",progress[0]+"");
}
protected void onPostExecute(Long result) {
exit();
}
}
};
t.start();
}
});
}
}
I'd like to brand the AsyncTask class for doing something in background updating the caller thread with the progress but with the caller thread != of UI thread.
I've tried that code but the line publishProgress(i) seems have not effect.
Can someone tell me how can I fix it (or what other class can I use else).
Thanks in advance =)
public class MainUI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Thread t=new Thread(){
boolean exit=false;
public void run(){
Looper.prepare();
new DownloadFilesTask().execute();
while (!exit){
try {
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d("","Exit thread");
Looper.loop();
}
public void exit(){
exit=true;
}
class DownloadFilesTask extends AsyncTask<Void, Long, Long> {
protected Long doInBackground(Void... urls) {
long i=0;
for (i=0;i<20;i++){
Log.d("",i+" ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(i);
}
return i;
}
protected void onProgressUpdate(Long... progress) {
Log.d("Test",progress[0]+"");
}
protected void onPostExecute(Long result) {
exit();
}
}
};
t.start();
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建线程来启动 ASyncTask 是没有用的,不应该这样做。
您应该阅读 android 开发人员网站上的无痛线程文档
http://android -developers.blogspot.com/2009/05/painless-threading.html
Creating a thread to start an ASyncTask is useless and should not be done.
You should read the painless threading document on the android developer site
http://android-developers.blogspot.com/2009/05/painless-threading.html
要使此类正常工作,必须遵循一些线程规则:
任务实例必须在 UI 线程上创建。
因此您不能在 UI 之外创建它线。请改用 Task 并将其包装在 ThreadPoolExecutor 对象中。请注意,在使用其中之一更新 UI 时,您需要使其成为线程安全的:
但再次强调,Asynctask 毫无用处,我不推荐它。
问候
There are a few threading rules that must be followed for this class to work properly:
The task instance must be created on the UI thread.
So you can NOT create it outside of the UI Thread. Use Task instead and wrap it in a ThreadPoolExecutor object. Be aware that you need to make it thread-safe when updating the UI by using one of those :
But once again, Asynctask is useless and I do NOT recommend it.
Regards