Day 15: Working in Background

15,531 views

Published on

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

No Downloads
Views
Total views
15,531
On SlideShare
0
From Embeds
0
Number of Embeds
13,540
Actions
Shares
0
Downloads
196
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Day 15: Working in Background

  1. 1. Android Application DevelopmentServiceAhsanul Karimahsanul.karim@sentinelbd.comSentinel Solutions Ltd.http://www.sentinelbd.com
  2. 2. Application Building Blocks• UI Component TypicallyCorresponding to one screen.Activity• Responds to notifications or statuschanges. Can wake up your process.IntentReceiver• Faceless task that runs in thebackground.Service• Enable applications to share data.ContentProvider
  3. 3. Android Application AnatomyActivities1. Provides User Interface2. Usually represents a Single Screen3. Can contain one/more Views4. Extends the Activity Base classServices1. No User Interface2. Runs in Background3. Extends the Service Base ClassApplication= Set of Android ComponentsContent Provider1. Makes application data availableto other apps2. Data stored in SQLite database3. Extends the ContentProviderBase classIntent/Broadcast Receiver1. Receives and Reacts to broadcastIntents2. No UI but can start an Activity3. Extends the BroadcastReceiverBase Class
  4. 4. ServiceWhat is Service?1. Services are codes that run in the background2. They can be started and stopped3. Services doesn’t have UIAudio ServicestartService()stopService()Audio Player UIPlays Audio
  5. 5. ServiceWhat a Service is NOT?There are some confusions:1. A Service is not a separate process. The Service object itself does not imply it isrunning in its own process; unless otherwise specified, it runs in the sameprocess as the application it is part of.2. A Service is not a thread. It is not a means itself to do work off of the mainthread (to avoid Application Not Responding errors).Main Features of Service1. to tell the system about something it wants to be doing in the background (even whenthe user is not directly interacting with the application)2. to calls to Context.startService(), which ask the system to schedule work for the service,to be run until the service or someone else explicitly stop it.
  6. 6. ServiceService ExampleWe’ll create a simple ServiceDemo application which runs in background and showsnotification in the upper Notification Bar with a period of specified time1. We create a project with following:Project Name: ServiceDemoBuild Target: 1.6Application name: ServiceDemoPackage name:com.basistraining.servicedemoCreate Activity:ServiceDemoActivityMin SDK Version: 4
  7. 7. ServiceService Example (Contd.)2. We’ll add a new class MyService that extends Service. We get the following.3. We’ll also need to add the Service in AndroidMenifest.xml4. Now we add other lifecycle methods of the MyService:1. onCreate()2. onStart()3. onDestroy()
  8. 8. ServiceService Example (Contd.)
  9. 9. ServiceService Example (Contd.)
  10. 10. ServiceService Example (Contd.)Now let’s make the Layout res/layout/main.xml to have 2 buttons to start and stop theServiceThere are only 2 buttons with id “@+id/btnStart” and “@+id/btnStop”
  11. 11. ServiceService Example (Contd.)Now we add action to our Buttons to Start or Stop the MyService and the Application in ouronCreate() method of the Activity
  12. 12. ServiceService Example (Contd.)If we run the app and test we’ll see our buttons are starting and stopping the service in LogCat
  13. 13. ServiceService Example (Contd.)Now to do something on Starting of our Service, we do following:Now to stop the timer, we do following:
  14. 14. ServiceService Example (Contd.)If we run the app and test we’ll see our buttons are starting and stopping the service in LogCat
  15. 15. ServiceService Example (Contd.)Now, Let we want to show a notification in the Notification Bar instead of just LogCat
  16. 16. ServiceService Example (Contd.)If we run the app and test we’ll see our buttons are starting and stopping the service inNotification

×