Android Insights - 2      Services
Topics to be Covered• Introduction to Services• Different Forms of Services• Running a Service in the Foreground2/11/2012 ...
Services• A Service is an application component that can  perform long-running operations in the background  and does not ...
Different Forms of Services• Started      – Performs a single operation and does not return a        result to the caller....
Different Forms of Services...                           Started Service                              Bound ServiceWhen to...
Started Service• 2 Types based on whether you need to  concurrently handle multiple start requests:      – Concurrency not...
Started Service...• Example of IntentService:2/11/2012           Android Insights - 2   7/13
Started Service...• Example of Service:2/11/2012           Android Insights - 2   8/13
Bound Service• 2 Types based on whether the service is accessed from  different processes:      – Service is private to yo...
Bound Service...• Example of Extending the Binder class:2/11/2012          Android Insights - 2    10/13
Bound Service...• An activity that binds to LocalService and calls  getRandomNumber() when a button is clicked:2/11/2012  ...
Bound Service...• An activity that binds to LocalService and calls  getRandomNumber() when a button is clicked (cont.):2/1...
Running a Service in Foreground•   A foreground service is a service thats considered to be something the user is    activ...
Upcoming SlideShare
Loading in...5
×

Android Insights - 2 [Services]

2,276

Published on

- Introduction to Android services
- Different forms of services (started, bound etc.)
- Developing services
- Foreground services.

Presentation by:
Sharafat Ibn Mollah Mosharraf
sharafat_8271@yahoo.co.uk
http://www.sharafat.co.cc

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,276
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
197
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Android Insights - 2 [Services]"

  1. 1. Android Insights - 2 Services
  2. 2. Topics to be Covered• Introduction to Services• Different Forms of Services• Running a Service in the Foreground2/11/2012 Android Insights - 2 2/13
  3. 3. Services• A Service is an application component that can perform long-running operations in the background and does not provide a user interface.• A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work.2/11/2012 Android Insights - 2 3/13
  4. 4. Different Forms of Services• Started – Performs a single operation and does not return a result to the caller. – For example, it might download or upload a file over the network.• Bound – Offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC).2/11/2012 Android Insights - 2 4/13
  5. 5. Different Forms of Services... Started Service Bound ServiceWhen to use If the application doesn’t If the application or other need to communicate with applications need to communicate the service after starting it. with the service after starting it.Method called to startService() bindService()start the serviceLifetime Once started, a service can Runs only as long as another run in the background application component is bound to indefinitely, even if the it. component that started it is destroyed. Multiple components can bind to the service at once, but when all of When the operation is done, them unbind, the service is the service should stop itself. destroyed.2/11/2012 Android Insights - 2 5/13
  6. 6. Started Service• 2 Types based on whether you need to concurrently handle multiple start requests: – Concurrency not needed • Extend the IntentService class. • Multiple requests will be automatically queued and handled one after another. – Concurrency needed • Extend the Service class. • You need to handle multiple requests concurrently.2/11/2012 Android Insights - 2 6/13
  7. 7. Started Service...• Example of IntentService:2/11/2012 Android Insights - 2 7/13
  8. 8. Started Service...• Example of Service:2/11/2012 Android Insights - 2 8/13
  9. 9. Bound Service• 2 Types based on whether the service is accessed from different processes: – Service is private to your process • Create your interface by extending the Binder class and returning an instance of it from onBind(). • The client receives the Binder and can use it to directly access public methods available in either the Binder implementation or even the Service. – Service can be accessed from different processes • Yet again 2 types based on whether multiple requests need to be handled concurrently or not. • Concurrency not needed: Use a Messenger. • Concurrency needed: Use AIDL (Android Interface Definition Language).2/11/2012 Android Insights - 2 9/13
  10. 10. Bound Service...• Example of Extending the Binder class:2/11/2012 Android Insights - 2 10/13
  11. 11. Bound Service...• An activity that binds to LocalService and calls getRandomNumber() when a button is clicked:2/11/2012 Android Insights - 2 11/13
  12. 12. Bound Service...• An activity that binds to LocalService and calls getRandomNumber() when a button is clicked (cont.):2/11/2012 Android Insights - 2 12/13
  13. 13. Running a Service in Foreground• A foreground service is a service thats considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory.• A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.• For example, a music player that plays music from a service should be set to run in the foreground, because the user is explicitly aware of its operation. The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player.• To request that your service run in the foreground, call startForeground().• To remove the service from the foreground, call stopForeground().• Note: The methods startForeground() and stopForeground() were introduced in Android 2.0 (API Level 5). In order to run your service in the foreground on older versions of the platform, you must use the previous setForeground() method—see the startForeground() documentation for information about how to provide backward compatibility.2/11/2012 Android Insights - 2 13/13
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×