SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

Android animation
Android animationAndroid animation
Android animation
Krazy Koder
 
Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)
Ahsanul Karim
 
android content providers
android content providersandroid content providers
android content providers
Deepa Rani
 
android layouts
android layoutsandroid layouts
android layouts
Deepa Rani
 
Android resource
Android resourceAndroid resource
Android resource
Krazy Koder
 

La actualidad más candente (20)

Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
 
Mobile Application Development With Android
Mobile Application Development With AndroidMobile Application Development With Android
Mobile Application Development With Android
 
Web Application Introduction
Web Application  IntroductionWeb Application  Introduction
Web Application Introduction
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
androidstudio.pptx
androidstudio.pptxandroidstudio.pptx
androidstudio.pptx
 
jQuery
jQueryjQuery
jQuery
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Android animation
Android animationAndroid animation
Android animation
 
Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)
 
Introduction to flutter
Introduction to flutter Introduction to flutter
Introduction to flutter
 
New Elements & Features in HTML5
New Elements & Features in HTML5New Elements & Features in HTML5
New Elements & Features in HTML5
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
android content providers
android content providersandroid content providers
android content providers
 
android layouts
android layoutsandroid layouts
android layouts
 
Notification android
Notification androidNotification android
Notification android
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Android intents
Android intentsAndroid intents
Android intents
 
Broadcast Receivers in Android
Broadcast Receivers in AndroidBroadcast Receivers in Android
Broadcast Receivers in Android
 
Android SDK Tutorial | Edureka
Android SDK Tutorial | EdurekaAndroid SDK Tutorial | Edureka
Android SDK Tutorial | Edureka
 
Android resource
Android resourceAndroid resource
Android resource
 

Similar a [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 a [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
 

Más de Nikmesoft Ltd

Más de 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
 

Último

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
 
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
 
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
 
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
 

Último (20)

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
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...
 
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
 
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 ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
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
 
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
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
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
 
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
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 

[Android] Services and Broadcast Receivers