Android Programming




        Lesson 11
Services and Broadcast
       Receivers
      NGUYEN The Linh
Android Programming


Contents



      1    Android Services

      2    Broadcast Receivers

      3    Services and Broadcast Receivers




                         2
Android Programming


Services and Broadcast Receivers




             Android Services




                    3
Android Programming


Android Services

 What is a service?
                           Download the
                            file in the
                           background.


     Keeps playing
      song in the
      background




                       4
Android Programming


Android Services

 What is a service?
   A Service is an application component that can perform long-
    running operations in the background and does not provide a user
    interface.

    A Service can run in the background to perform work even while
     the user is in a different application.

    A Service runs in the main thread of the application that hosts it,
     by default.

                                  5
Android Programming


Android Services

 What is a service?
   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.




                                 6
Android Programming


Android Services

 Creating a service
    To create a service, you must create a subclass of Service.




                                 7
Android Programming


Android Services

 Creating a service
    Now we will override two more methods: onStart and onDestroy




                                8
Android Programming


Android Services

 Creating a service
    Necessary to let the AndroidManifest file know about your
     service <service android:enabled="true"
     android:name=".DemoService" />




                                9
Android Programming


Android Services

 Starting a Service
    The application can start the service with the help of
     Context.startService method. The method will call the onCreate
     method of the service if service is not already created; else
     onStart method will be called.
    Context.startService() | ->onCreate() – >onStartCommand()
     [service running]




                               10
Android Programming


Android Services

 Stopping a service
    The service started with startService method will keep on running
     until stopService() is called or stopSelf() method is called.
    context.stopService() | ->onDestroy() [service stops]




                                 11
Android Programming


Android Services

 Stopping a service
    It's important that your application stops its services when it's done
     working, to avoid wasting system resources and consuming
     battery power.




                                   12
Android Programming


Android Services

 Service Life Cycle
    The lifecycle of a service is much simpler than that of an activity.
     However, it's even more important that you pay close attention to
     how your service is created and destroyed, because a service can
     run in the background without the user being aware.

     The service lifecycle—from when it's created to when it's
      destroyed—can follow two different paths:
        • A started service
        • A bound service


                                  13
Android Programming


Android Services

 Service Life Cycle
                    Runs indefinitely


                                             stopService()
   startService()
                        Service




                       stopSelf()


                                        14
Android Programming


Android Services

 Service Life Cycle




   bindService()
                      Service
 unbindService()



           when all of them unbind


                                     15
Android Programming


Android Services

 Service Life Cycle




                       16
Android Programming


Android Services

 Example 11.1




                   17
Android Programming


Services and Broadcast Receivers




            Broadcast Receivers




                    18
Android Programming


Broadcast Receivers

 What is a broadcast receiver?
   A broadcast receiver is an Android component which allows to
    register for system or application events. All registered
    receivers for an event will be notified by Android once this event
    happens.
   For example Android allows that applications can register for
    the ACTION_BOOT_COMPLETED which is fired by the system
    once the Android system has completed the boot process.




                                  19
Android Programming


Broadcast Receivers

 What is a broadcast receiver?

              Application 1                             Application 2

BroadcastReceiver-1   BroadcastReceiver-1             BroadcastReceiver-3
      Filter-2              Filter-1                        Filter-2




        Service-1                         Service-2
         Filter-1                          Filter-2


                                     20
Android Programming


Broadcast Receivers

 Registering a broadcast receiver?
    To create a broadcast receiver, you must create a subclass
     of BroadcastReceiver




                                21
Android Programming


Broadcast Receivers

 Registering a broadcast receiver?
    Register a Receiver dynamically in java code.




                                22
Android Programming


Broadcast Receivers

 Registering a broadcast receiver?
    Or let the AndroidManifest file know about your receiver




                                23
Android Programming


Broadcast Receivers

 BroadcastReceiver life cycle?

                Get notified when Intent happens



      Android                                      Broadcast
      System                                        Receiver



                  Registers for certain intents


                              24
Android Programming


Broadcast Receivers

 Example 11.2




                      25
Android Programming


Services and Broadcast Receivers




      Services and Broadcast Receivers




                    26
Android Programming


Services and Broadcast Receivers



Services-1        Broadcast
                  Receiver-1
                   Filter-1



                   Activity          Broadcast
                                     Receiver-2
                                      Filter-2



                    27
Android Programming

[Android] Services and Broadcast Receivers