阅读更多 转自[url] http://javatechig.com/android/creating-a-background-service-in-android[/url]


1. What is IntentService?

IntentService is a subclass of android.app.Service class. A stated intent service allows to handle long running tasks without effecting the application UI thread. This is not bound to any activity so, it is not getting effected for any change in activity lifecycle. Once IntentService is started, it handles each Intent using a worker thread and stops itself when it runs out of work.

IntentService would be an best solution, If you have an work queue to process. For example, if your application using analytics you will likely to send event name and related parameter to your tracking server for each user generated event. Although each event means a tiny piece of data, creating networking request on each click will result an overhead to your application. Instead, you can use work queue processor design pattern and process the events in a batch.
2. IntentService Limitations

    No easy or direct way to interact with user interface directly from IntentService. Later in this example, we will explain to pass result back from IntentService to
    With IntentService, there can only be one request processed at any single point of time. If you request for another task, then the new job will wait until the previous one is completed. This means that IntentService process the request
    An tasks stated using IntentService cannot be interrupted

3. Why do we need IntentService?

Android design guidelines strongly suggests to perform all the long running tasks off the UI thread. For example, if you have to periodically download the largest chunk of data from server, you must use IntentService to avoid ANR. ANR (Application not responding) message often occurs, if your main thread is doing too much of work. In this course of this tutorial, we will learn the below concepts

    How to create and use IntentService
    How to pass data from activity to service as parameter
    How to pass result back to activity
    Update activity based on the result

Case Study

To make this tutorial easy to understand we will extend our previous tutorial (Android Networking Tutorial) to use Intent Service for downloading the data from server. We suggest you to checkout Android Networking Example to get familiar with downloading data from server using different http clients available in Android.

Feed Url : http://javatechig.com/api/get_category_posts/?dev=1&slug=android

Expected Result Start service to download the data when application is started. Once download is complete, update ListView present in your activity.
  • Android-IntentService-Example-master.zip (113.1 KB)
  • 下载次数: 0

更多相关文章

  1. Android中文文档v0.1 beta低调发布,期待更多同学来参加review
  2. Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公
  3. Android recyclerview实现查看更多/收起功能
  4. Android ListView列表 刷新和加载更多
  5. Jquery点击加载更多
  6. 《锋利的jQuery》读书笔记 第5章 jQuery对表单、表格的操作及更
  7. 关于jQuery Themeroller主题,我在哪里可以找到更多?
  8. DataTables警告:table id = DataTables_Table_0 - Ajax错误。有
  9. 总结一些更多的针对webkit的HTML, CSS和Javascript方面的特性.

随机推荐

  1. android没有告诉你的usr模式和eng模式的
  2. Android之App界面的挂载与显示及源码分析
  3. Android程序猿:“我有五年开发经验”“不,
  4. 资源分享-良心资源,适合初,中级程序员
  5. 阿里大牛5年零3个月27天的Android(安卓)
  6. Android实现记事本项目完整实例,附源代码
  7. 第三章:Creating Applications and activi
  8. Android(安卓)开发中踩过的坑之一: Remot
  9. 2020年Android最新知识体系最强总结(全方
  10. Android(安卓)模块化、组件化、插件化及