首页 > 系统 > Android > 正文

android多线程 : asynctask,thread,handler

2019-11-06 08:28:33
字体:
来源:转载
供稿:网友

1.使用AsyncTask来运行异步任务 AsyncTask类为将耗时的操作移到后台线程并在操作完成后同步更新UI线程,实现了最佳实践模式。AsyncTask处理线程创建,管理和同步等全部工作。 1.1 创建一个异步任务(将在后台执行的处理和处理完成后UI的更新) 这里写图片描述 1.2 执行异步任务

String input = “hahahahahah”;New MyAsyncTask().execute(input)

2.使用java中的Thread来管理多线程 UI线程中调用

PRivate void backGroundExecution(){Thread thread = new Thread(null, doBackgroundThreadProcessing, “Background”);Thread.start();}后台线程Private Runnable doBackgroundThreadProcessing = new Runnable(){Public void run(){backgroundThreadProcessing();}}Private void backgroundThreadProcessing(){//do something need lot time}

3.使用android Handler 这里写图片描述 使用postDelayed延迟发布更新

handler.postDelayed(doUpadateGUI, 1000);

使用postAtTime定时发布更新

Handler.postAtTime(doUpadateGUI, SystemClock.upTImeMillis());
上一篇:android : service

下一篇:android:Intent组件

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表