Android使用IntentService执行异步任务
2016-08-15 23:07
555 查看
启动服务后,待服务执行完onHandleIntent 会自动退出
public class MyIntentService extends IntentService { @Override public void onDestroy() { super.onDestroy(); Log.i("Service","MyIntentServiceDestroy"); } public MyIntentService() { super("MyIntentServiceName"); } @Override protected void onHandleIntent(Intent intent) { try{ int result = DownloadFile(new URL("http://www.x-motion.com/somefile.pdf")); Log.i("Service","Download "+result +" bytes"); } catch (MalformedURLException e) { e.printStackTrace(); } } private int DownloadFile(URL url){ try{ Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return 100; } }
相关文章推荐
- 【Android 开发教程】使用IntentService执行异步操作
- Android Training - 使用IntentService执行任务(Lesson 1 - 创建IntentService)
- Android Training - 使用IntentService执行任务(Lesson 2 - 发送任务给IntentService)
- Android Training - 使用IntentService执行任务(Lesson 3 - 回传任务状态给发送方)
- Service学习——第五篇 使用IntentService在单独线程上执行异步任务
- Android中使用IntentService执行后台任务
- Android中使用IntentService执行后台任务
- Android使用IntentService执行耗时任务
- 使用IntentService执行后台任务
- Android中使用IntentService运行后台任务
- Android中使用IntentService运行后台任务
- Android Training - 使用IntentService运行任务(Lesson 2 - 发送任务给IntentService)
- Android IntentService 可执行耗时任务的Service
- Android中AsyncTask(异步执行任务)的使用
- Android Training - 使用IntentService运行任务(Lesson 1 - 创建IntentService)
- Android中使用Handler和异步任务(AsyncTack)来为UI线程执行费时操作
- Android中使用Handler和异步任务(AsyncTack)来为UI线程执行费时操作
- 线程相关——HandlerThread、IntentService、ResultReceiver:结果接收者、AsyncTask:异步任务、Android中处理线程间通信的方式
- Android使用Intent启动Service的Intent必须是显式的