Android Application Development Service Android Application Anatomy Activities

  • Slides: 29
Download presentation
Android Application Development Service

Android Application Development Service

Android Application Anatomy Activities Services 1. Provides User Interface 1. No User Interface 2.

Android Application Anatomy Activities Services 1. Provides User Interface 1. No User Interface 2. Usually represents a Single Screen 2. Runs in Background 3. Can contain one/more Views 3. Extends the Service Base Class 4. Extends the Activity Base class Application= Set of Android Components Broadcast Receiver 1. Receives and Reacts to broadcast Intents 2. No UI but can start an Activity 3. Extends the Broadcast. Receiver Base Class Content Provider 1. Makes application data available to other apps 2. Data stored in SQLite database 3. Extends the Content. Provider Base class

What is not a Service • Not a separate process • Not a thread

What is not a Service • Not a separate process • Not a thread

What a Service is • Long running operation in background • No UI interaction

What a Service is • Long running operation in background • No UI interaction

Two way Lifecycle • Start Service • Bind Service • Both

Two way Lifecycle • Start Service • Bind Service • Both

Start Service • Start by other application component such like (Activity, Service etc. )

Start Service • Start by other application component such like (Activity, Service etc. ) • Destroy by calling stop. Service() or stop. Self()

Bind Service • Bind when application component call bind. Service() • Unbind when call

Bind Service • Bind when application component call bind. Service() • Unbind when call unbind. Service() • Provide IBinder to interact with other component • When none is bind, Service destroy

Callback Methods • • on. Start. Command() on. Bind() on. Create() on. Destroy()

Callback Methods • • on. Start. Command() on. Bind() on. Create() on. Destroy()

on. Start. Command() • When Service is start by calling start. Service(), on. Start.

on. Start. Command() • When Service is start by calling start. Service(), on. Start. Command will be called • The Service will indefinitely running • You have to call stop. Self() or stop. Service() to destroy Service

on. Bind() • Help to bind Service and other Application Component by Serive. Connection

on. Bind() • Help to bind Service and other Application Component by Serive. Connection • Return an Ibinder instance to interact with other component • Return null to not allow binding

Service Connection • on. Service. Connected() – (Component. Name name, IBinder service) • on.

Service Connection • on. Service. Connected() – (Component. Name name, IBinder service) • on. Service. Disconnected() – (Component. Name name)

on. Create() • Called before on. Start. Command() and on. Bind() • Won’t call

on. Create() • Called before on. Start. Command() and on. Bind() • Won’t call if Service is already running

on. Destroy() • Called when the Service is no longer used • Should clean

on. Destroy() • Called when the Service is no longer used • Should clean up all the resource such as thread, listener or receivers

Stop a Service • stop. Service(Intent intent) • stop. Self(int start. Id)

Stop a Service • stop. Service(Intent intent) • stop. Self(int start. Id)

Manifest <manifest. . . >. . . <application. . . > <service android: name=".

Manifest <manifest. . . >. . . <application. . . > <service android: name=". My. Service" />. . . </application> </manifest>

Intent Service

Intent Service

Intent Service • Extends Intent. Service • Auto Generate worker thread • Generate work

Intent Service • Extends Intent. Service • Auto Generate worker thread • Generate work queue to avoid multithreading

Intent Service • Auto stop Service when all request is done • Default on.

Intent Service • Auto stop Service when all request is done • Default on. Bind() return null • Default on. Start. Command() redirected to on. Handle. Intent()

Service vs Activity • No UI • Won’t killed when screen rotate

Service vs Activity • No UI • Won’t killed when screen rotate

Service vs Thread • Main thread or not • Context embedded • More. .

Service vs Thread • Main thread or not • Context embedded • More. .

Service vs Intent Service • Intent Service provide worker queue that only allow one

Service vs Intent Service • Intent Service provide worker queue that only allow one thread to execute at one time • Intent. Service stops itself when requested tasks completes but Service should be stopped manually • More. .

Service Example We’ll create a simple Service. Demo application which runs in background and

Service Example We’ll create a simple Service. Demo application which runs in background and shows notification in the upper Notification Bar with a period of specified time 1. We create a project with following: Project Name: Service. Demo Build Target: 1. 6 Application name: Service. Demo Package name: com. basistraining. servicedemo Create Activity: Service. Demo. Activity Min SDK Version: 4

Service Example

Service Example

Service Example (Contd. )

Service Example (Contd. )

Service Example (Contd. ) Now let’s make the Layout res/layout/main. xml to have 2

Service Example (Contd. ) Now let’s make the Layout res/layout/main. xml to have 2 buttons to start and stop the Service There are only 2 buttons with id “@+id/btn. Start” and “@+id/btn. Stop”

Service Example (Contd. ) Now we add action to our Buttons to Start or

Service Example (Contd. ) Now we add action to our Buttons to Start or Stop the My. Service and the Application in our on. Create() method of the Activity

Service Example (Contd. ) If we run the app and test we’ll see our

Service Example (Contd. ) If we run the app and test we’ll see our buttons are starting and stopping the service in Log. Cat

Service Example (Contd. ) Now to do something on Starting of our Service, we

Service Example (Contd. ) Now to do something on Starting of our Service, we do following: Now to stop the timer, we do following:

Service Example (Contd. ) If we run the app and test we’ll see our

Service Example (Contd. ) If we run the app and test we’ll see our buttons are starting and stopping the service in Log. Cat