KEMBAR78
Android Services | PPTX
Android Application DevelopmentServiceAhsanul Karimahsanul.karim@sentinelbd.comSentinel Solutions Ltd.http://www.sentinelbd.com
Application Building Blocks
Android Application AnatomyActivitiesProvides User InterfaceUsually represents a Single ScreenCan contain one/more ViewsExtendsthe Activity Base classServicesNoUser InterfaceRuns in BackgroundExtends the Service Base ClassIntent/Broadcast ReceiverReceives and Reacts to broadcast IntentsNo UI but can start an ActivityExtends the BroadcastReceiver Base ClassContent ProviderMakes application data available to other appsData stored in SQLite databaseExtends the ContentProviderBase classApplication= Set of Android Components
ServiceWhat is Service?Services are codes that run in the backgroundThey can be started and stoppedServices doesn’t have UIstartService()Audio ServicePlays AudiostopService()Audio Player UI
ServiceWhat a Service is NOT?There are some confusions: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 same process as the application it is part of. A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors). Main Features of Serviceto tell the system about something it wants to be doing in the background (even when the user is not directly interacting with the application)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.
ServiceService ExampleWe’ll create a simple ServiceDemo application which runs in background and shows notification in the upper Notification Bar with a period of specified timeWe create a project with following:Project Name: ServiceDemoBuild Target: 1.6Application name:ServiceDemoPackage name: com.basistraining.servicedemoCreate Activity:ServiceDemoActivityMin SDK Version: 4
ServiceService Example (Contd.)2. We’ll add a new class MyServicethat 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: onCreate()onStart()onDestroy()
ServiceService Example (Contd.)
ServiceService Example (Contd.)
ServiceService Example (Contd.)Now let’s make the Layout res/layout/main.xml to have 2 buttons to start and stop the ServiceThere are only 2 buttons with id “@+id/btnStart” and“@+id/btnStop”
ServiceService Example (Contd.)Now we add action to our Buttons to Start or Stop the MyService and the Application in our onCreate() method of the Activity
ServiceService Example (Contd.)If we run the app and test we’ll see our buttons are starting and stopping the service in LogCat
ServiceService Example (Contd.)Now to do something on Starting of our Service, we do following:Now to stop the timer, we do following:
ServiceService Example (Contd.)If we run the app and test we’ll see our buttons are starting and stopping the service in LogCat
ServiceService Example (Contd.)Now, Let we want to show a notification in the Notification Bar instead of just LogCat
ServiceService Example (Contd.)If we run the app and test we’ll see our buttons are starting and stopping the service in Notification

Android Services

  • 1.
    Android Application DevelopmentServiceAhsanulKarimahsanul.karim@sentinelbd.comSentinel Solutions Ltd.http://www.sentinelbd.com
  • 2.
  • 3.
    Android Application AnatomyActivitiesProvidesUser InterfaceUsually represents a Single ScreenCan contain one/more ViewsExtendsthe Activity Base classServicesNoUser InterfaceRuns in BackgroundExtends the Service Base ClassIntent/Broadcast ReceiverReceives and Reacts to broadcast IntentsNo UI but can start an ActivityExtends the BroadcastReceiver Base ClassContent ProviderMakes application data available to other appsData stored in SQLite databaseExtends the ContentProviderBase classApplication= Set of Android Components
  • 4.
    ServiceWhat is Service?Servicesare codes that run in the backgroundThey can be started and stoppedServices doesn’t have UIstartService()Audio ServicePlays AudiostopService()Audio Player UI
  • 5.
    ServiceWhat a Serviceis NOT?There are some confusions: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 same process as the application it is part of. A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors). Main Features of Serviceto tell the system about something it wants to be doing in the background (even when the user is not directly interacting with the application)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.
    ServiceService ExampleWe’ll createa simple ServiceDemo application which runs in background and shows notification in the upper Notification Bar with a period of specified timeWe create a project with following:Project Name: ServiceDemoBuild Target: 1.6Application name:ServiceDemoPackage name: com.basistraining.servicedemoCreate Activity:ServiceDemoActivityMin SDK Version: 4
  • 7.
    ServiceService Example (Contd.)2.We’ll add a new class MyServicethat 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: onCreate()onStart()onDestroy()
  • 8.
  • 9.
  • 10.
    ServiceService Example (Contd.)Nowlet’s make the Layout res/layout/main.xml to have 2 buttons to start and stop the ServiceThere are only 2 buttons with id “@+id/btnStart” and“@+id/btnStop”
  • 11.
    ServiceService Example (Contd.)Nowwe add action to our Buttons to Start or Stop the MyService and the Application in our onCreate() method of the Activity
  • 12.
    ServiceService Example (Contd.)Ifwe run the app and test we’ll see our buttons are starting and stopping the service in LogCat
  • 13.
    ServiceService Example (Contd.)Nowto do something on Starting of our Service, we do following:Now to stop the timer, we do following:
  • 14.
    ServiceService Example (Contd.)Ifwe run the app and test we’ll see our buttons are starting and stopping the service in LogCat
  • 15.
    ServiceService Example (Contd.)Now,Let we want to show a notification in the Notification Bar instead of just LogCat
  • 16.
    ServiceService Example (Contd.)Ifwe run the app and test we’ll see our buttons are starting and stopping the service in Notification