Presentation is loading. Please wait.

Presentation is loading. Please wait.

Services. What is a Service? A Service is not a separate process. A Service is not a thread. A Service itself is actually very simple, providing two main.

Similar presentations


Presentation on theme: "Services. What is a Service? A Service is not a separate process. A Service is not a thread. A Service itself is actually very simple, providing two main."— Presentation transcript:

1 Services

2 What is a Service? A Service is not a separate process. A Service is not a thread. A Service itself is actually very simple, providing two main features: A facility for the application to tell the system about something it wants to be doing in the background. A facility for an application to expose some of its functionality to other applications.

3 You should use Android Services when you have to perform a time consuming and long task, like loading an Image, or a File, or download something for the Internet and asynchronous tasks in general. Using a Service you can let the UI Thread handle only UI tasks.

4 there are two types of Services: Unbounded : This type of Service is also called Started. This Service is completely independent for the Activity that called it and there is no communication or interaction between them. The Service simply starts (using startService), does its job and stops(using stopService) without the Activity noticing anything. Bounded : This type of Service offers communication and interaction between the Service and the Activity that launched it. Using Messagers and BroadcastReceivers the Activity can at any moment monitor the status or the progress of the background task that the Service is performing. Additionally the Service can get useful feedback from the Activity.

5 When a Service component is actually created, for either of above reasons, all that the system actually does is instantiate the component and call its onCreate() and any other appropriate callbacks on the main thread. It is up to the Service to implement these with the appropriate behavior, such as creating a secondary thread in which it does its work.

6 Service Lifecycle There are two reasons that a service can be run by the system. On call Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed). Then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called.

7

8

9 On multiple click or multiple call… multiple calls to Context.startService() do not nest Though they do result in multiple corresponding calls to onStartCommand(). How many times it is started a service will be stopped once Context.stopService() or stopSelf() is called. Services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed.

10 For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed. START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them.

11 Clients can also use Context.bindService() to obtain a persistent connection to a service. This creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand() in the same way. The client will receive the IBinder object that the service returns from its onBind(Intent) method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established

12 Permissions Global access to a service can be enforced when it is declared in its manifest's tag. when using Context.startService(Intent), you can also set Intent.FLAG_GRANT_READ_URI_PERMISSION and/or Intent.FLAG_GRANT_WRITE_URI_PERMISSION on the Intent. This will grant the Service temporary access to the specific URIs in the Intent.

13 Process Lifecycle The Android system will attempt to keep the process hosting a service around as long as the service has been started or has clients bound to it. When running low on memory and needing to kill existing processes, the priority of a process hosting the service will be the higher of the following possibilities:

14 If the service is currently executing code in its onCreate(), onStartCommand(), or onDestroy() methods, then the hosting process will be a foreground process to ensure this code can execute without being killed. If the service has been started, then its hosting process is considered to be less important than any processes that are currently visible to the user on-screen, but more important than any process not visible.(should not be killed except in low memory conditions).

15 If there are clients bound to the service, then the service's hosting process is never less important. A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory.

16 In short about services.. Services in Android is a component that runs in the background without any user interaction or interface. Unlike Activities services does not have any graphical interface. Services run invisibly performing long running tasks - doing Internet look ups, playing music, triggering notifications etc., Services run with a higher priority than inactive or invisible activities and therefore it is less likely that the Android system terminates them for resource management.

17 The only reason Android will stop a Service prematurely is to provide additional resources for a foreground component usually an Activity. When this happens, your Service can be configured to restart automatically.

18 Creating a Service Create a new class that extends Service. Below is the code snippet.

19 create a simple example to create, start and stop android service. create a layout file activity_main.xml, MainActivity and MyService class files. In MainActivity have just started and stopped MyService class through Intent on button click event. In MyService class display Toast messages for onCreate(), onStart(), onDestroy() events.

20

21 activity_main.xml

22 MainActivity.java

23 MyService.java

24 AndroidManifest.xml

25


Download ppt "Services. What is a Service? A Service is not a separate process. A Service is not a thread. A Service itself is actually very simple, providing two main."

Similar presentations


Ads by Google