SlideShare a Scribd company logo
1 of 28
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

More Related Content

What's hot

android content providers
android content providersandroid content providers
android content providers
Deepa Rani
 
android activity
android activityandroid activity
android activity
Deepa Rani
 
Android datastorage
Android datastorageAndroid datastorage
Android datastorage
Krazy Koder
 
Android notification
Android notificationAndroid notification
Android notification
Krazy Koder
 

What's hot (20)

Android Intent.pptx
Android Intent.pptxAndroid Intent.pptx
Android Intent.pptx
 
Introduction to Android development - Presentation
Introduction to Android development - PresentationIntroduction to Android development - Presentation
Introduction to Android development - Presentation
 
Android testing
Android testingAndroid testing
Android testing
 
Android intents
Android intentsAndroid intents
Android intents
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
 
android content providers
android content providersandroid content providers
android content providers
 
android activity
android activityandroid activity
android activity
 
AndroidManifest
AndroidManifestAndroidManifest
AndroidManifest
 
Location-Based Services on Android
Location-Based Services on AndroidLocation-Based Services on Android
Location-Based Services on Android
 
Shared preferences
Shared preferencesShared preferences
Shared preferences
 
Activity lifecycle
Activity lifecycleActivity lifecycle
Activity lifecycle
 
Android Lesson 3 - Intent
Android Lesson 3 - IntentAndroid Lesson 3 - Intent
Android Lesson 3 - Intent
 
Android datastorage
Android datastorageAndroid datastorage
Android datastorage
 
05 intent
05 intent05 intent
05 intent
 
Basic android-ppt
Basic android-pptBasic android-ppt
Basic android-ppt
 
android menus
android menusandroid menus
android menus
 
Android Layout.pptx
Android Layout.pptxAndroid Layout.pptx
Android Layout.pptx
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studio
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Android notification
Android notificationAndroid notification
Android notification
 

Similar to [Android] Services and Broadcast Receivers

Android basic principles
Android basic principlesAndroid basic principles
Android basic principles
Henk Laracker
 
Os eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdfOs eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdf
weerabahu
 

Similar to [Android] Services and Broadcast Receivers (20)

Introduction toAndroid Programming=Architecture, Basic building blocks, API ,...
Introduction toAndroid Programming=Architecture, Basic building blocks, API ,...Introduction toAndroid Programming=Architecture, Basic building blocks, API ,...
Introduction toAndroid Programming=Architecture, Basic building blocks, API ,...
 
Android basic principles
Android basic principlesAndroid basic principles
Android basic principles
 
My android
My androidMy android
My android
 
My android
My androidMy android
My android
 
Android seminar report
Android seminar reportAndroid seminar report
Android seminar report
 
Android summer training report
Android summer training reportAndroid summer training report
Android summer training report
 
Android summer training report
Android summer training reportAndroid summer training report
Android summer training report
 
Android AppDevelopment
Android AppDevelopmentAndroid AppDevelopment
Android AppDevelopment
 
Android app Development Prepration Tips
Android app Development Prepration TipsAndroid app Development Prepration Tips
Android app Development Prepration Tips
 
[Android] Introduction to Android Programming
[Android] Introduction to Android Programming[Android] Introduction to Android Programming
[Android] Introduction to Android Programming
 
cpuk10745
cpuk10745cpuk10745
cpuk10745
 
Android Basics
Android BasicsAndroid Basics
Android Basics
 
Introduction To android
Introduction To androidIntroduction To android
Introduction To android
 
Basic of Android App Development
Basic of Android App DevelopmentBasic of Android App Development
Basic of Android App Development
 
Android101
Android101Android101
Android101
 
Android Basic Presentation (Introduction)
Android Basic Presentation (Introduction)Android Basic Presentation (Introduction)
Android Basic Presentation (Introduction)
 
[Android] Intent and Activity
[Android] Intent and Activity[Android] Intent and Activity
[Android] Intent and Activity
 
Os eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdfOs eclipse-androidwidget-pdf
Os eclipse-androidwidget-pdf
 
Introduction to android applications stu
Introduction to android applications stuIntroduction to android applications stu
Introduction to android applications stu
 
Android ppt
Android pptAndroid ppt
Android ppt
 

More from Nikmesoft Ltd

More from Nikmesoft Ltd (16)

[iOS] Networking
[iOS] Networking[iOS] Networking
[iOS] Networking
 
[iOS] Data Storage
[iOS] Data Storage[iOS] Data Storage
[iOS] Data Storage
 
[iOS] Multiple Background Threads
[iOS] Multiple Background Threads[iOS] Multiple Background Threads
[iOS] Multiple Background Threads
 
[iOS] Navigation
[iOS] Navigation[iOS] Navigation
[iOS] Navigation
 
[iOS] Basic UI Elements
[iOS] Basic UI Elements[iOS] Basic UI Elements
[iOS] Basic UI Elements
 
[iOS] Introduction to iOS Programming
[iOS] Introduction to iOS Programming[iOS] Introduction to iOS Programming
[iOS] Introduction to iOS Programming
 
[Android] Multimedia Programming
[Android] Multimedia Programming[Android] Multimedia Programming
[Android] Multimedia Programming
 
[Android] Android Animation
[Android] Android Animation[Android] Android Animation
[Android] Android Animation
 
[Android] 2D Graphics
[Android] 2D Graphics[Android] 2D Graphics
[Android] 2D Graphics
 
[Android] Web services
[Android] Web services[Android] Web services
[Android] Web services
 
[Android] Multiple Background Threads
[Android] Multiple Background Threads[Android] Multiple Background Threads
[Android] Multiple Background Threads
 
[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based Services[Android] Maps, Geocoding and Location-Based Services
[Android] Maps, Geocoding and Location-Based Services
 
[Android] Data Storage
[Android] Data Storage[Android] Data Storage
[Android] Data Storage
 
[Android] Widget Event Handling
[Android] Widget Event Handling[Android] Widget Event Handling
[Android] Widget Event Handling
 
[Android] Using Selection Widgets
[Android] Using Selection Widgets[Android] Using Selection Widgets
[Android] Using Selection Widgets
 
[Android] Basic Widgets and Containers
[Android] Basic Widgets and Containers[Android] Basic Widgets and Containers
[Android] Basic Widgets and Containers
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

[Android] Services and Broadcast Receivers