[Android] Services and Broadcast Receivers

705
-1

Published on

1. Android Services
1.1 What is a service?
1.2 Creating a service
1.3 Starting a Service
1.4 Stopping a service
1.5 Service Life Cycle
2. Broadcast Receivers
2.1 What is a broadcast receiver?
2.2 Registering a broadcast receiver?
2.3 BroadcastReceiver life cycle?
3. Services and Broadcast Receivers

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
705
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

[Android] Services and Broadcast Receivers

  1. 1. Android Programming Lesson 11Services and Broadcast Receivers NGUYEN The Linh
  2. 2. Android ProgrammingContents 1 Android Services 2 Broadcast Receivers 3 Services and Broadcast Receivers 2
  3. 3. Android ProgrammingServices and Broadcast Receivers Android Services 3
  4. 4. Android ProgrammingAndroid Services What is a service? Download the file in the background. Keeps playing song in the background 4
  5. 5. Android ProgrammingAndroid 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
  6. 6. Android ProgrammingAndroid 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
  7. 7. Android ProgrammingAndroid Services Creating a service  To create a service, you must create a subclass of Service. 7
  8. 8. Android ProgrammingAndroid Services Creating a service  Now we will override two more methods: onStart and onDestroy 8
  9. 9. Android ProgrammingAndroid Services Creating a service  Necessary to let the AndroidManifest file know about your service <service android:enabled="true" android:name=".DemoService" /> 9
  10. 10. Android ProgrammingAndroid 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
  11. 11. Android ProgrammingAndroid 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
  12. 12. Android ProgrammingAndroid Services Stopping a service  Its important that your application stops its services when its done working, to avoid wasting system resources and consuming battery power. 12
  13. 13. Android ProgrammingAndroid Services Service Life Cycle  The lifecycle of a service is much simpler than that of an activity. However, its 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 its created to when its destroyed—can follow two different paths: • A started service • A bound service 13
  14. 14. Android ProgrammingAndroid Services Service Life Cycle Runs indefinitely stopService() startService() Service stopSelf() 14
  15. 15. Android ProgrammingAndroid Services Service Life Cycle bindService() Service unbindService() when all of them unbind 15
  16. 16. Android ProgrammingAndroid Services Service Life Cycle 16
  17. 17. Android ProgrammingAndroid Services Example 11.1 17
  18. 18. Android ProgrammingServices and Broadcast Receivers Broadcast Receivers 18
  19. 19. Android ProgrammingBroadcast 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
  20. 20. Android ProgrammingBroadcast Receivers What is a broadcast receiver? Application 1 Application 2BroadcastReceiver-1 BroadcastReceiver-1 BroadcastReceiver-3 Filter-2 Filter-1 Filter-2 Service-1 Service-2 Filter-1 Filter-2 20
  21. 21. Android ProgrammingBroadcast Receivers Registering a broadcast receiver?  To create a broadcast receiver, you must create a subclass of BroadcastReceiver 21
  22. 22. Android ProgrammingBroadcast Receivers Registering a broadcast receiver?  Register a Receiver dynamically in java code. 22
  23. 23. Android ProgrammingBroadcast Receivers Registering a broadcast receiver?  Or let the AndroidManifest file know about your receiver 23
  24. 24. Android ProgrammingBroadcast Receivers BroadcastReceiver life cycle? Get notified when Intent happens Android Broadcast System Receiver Registers for certain intents 24
  25. 25. Android ProgrammingBroadcast Receivers Example 11.2 25
  26. 26. Android ProgrammingServices and Broadcast Receivers Services and Broadcast Receivers 26
  27. 27. Android ProgrammingServices and Broadcast ReceiversServices-1 Broadcast Receiver-1 Filter-1 Activity Broadcast Receiver-2 Filter-2 27
  28. 28. Android Programming

×