Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.

Similar presentations


Presentation on theme: "Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what."— Presentation transcript:

1 Cosc 5/4730 Android Services

2 What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what it is not: – A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of. – A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors). Thus 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 (even when the user is not directly interacting with the application). This corresponds to calls to Context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it.Context.startService() – A facility for an application to expose some of its functionality to other applications. This corresponds to calls to Context.bindService(), which allows a long-standing connection to be made to the service in order to interact with it.Context.bindService() http://developer.android.com/reference/android/app/Service.html

3 What is a service? (2) Basically a service can be though of as a data structure that runs. It doesn’t need a screen – Normally communicates with the activity that started it. It can run in parallel with an activity providing data and stuff. (binding) it may run without activity, to provide data for later use. – Say an alarm starts it every X minutes to check on something. Then may use a handler, notification, call a broadcast receiver, maybe even start an activity. Or my just write out the data to local storage, for later use.

4 What is a service? (3) Example – Use clicks on a picture that they want to download from the web into their gallery. – The application kicks off a downloader service and then the user continues. – The service downloads the picture and puts into the gallery. And creates a Notification when done that the picture has completed (or failed). The user can click on the notification and open the picture (using the gallery app).

5 What is a service? (4) Service – This is the base class for all services. When you extend this class, it's important that you create a new thread in which to do all the service's work, because the service uses your application's main thread, by default, which could slow the performance of any activity your application is running. IntentService – This is a subclass of Service that uses a worker thread to handle all start requests, one at a time. This is the best option if you don't require that your service handle multiple requests simultaneously. All you need to do is implement onHandleIntent(), which receives the intent for each start request so you can do the background work. – You can override other methods as needed like you would need in a Service.

6 IntentService Easy to implement Create a constructor with a super(“name”) Override onHandleIntent(Intent) { …} – Inside is the work to be done. – The Extra bundle tells you what “to process”. – Send broadcast intent or notification when “completed”. May also send a message back to the activity via handler by putting the messenger object in the Bundle!

7 IntentService Example public class myIntentService extends IntentService { required constructor public myIntentService() { super(“myIntentService"); } Where we do the work. @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); //now get the information you need to do whatever is needed. }

8 Call the IntentService My service “returns” X number of random numbers based on the intent, so Intent number5 = new Intent(getBaseContext(), myIntentService.class); number5.putExtra("times", 5); //5 random number Send numbers back through a messenger Messenger messenger = new Messenger(handler); number5.putExtra("MESSENGER", messenger); If no MESSENGER key, then the service will use notifications. startService(number5); //start the service.

9 Service. Far more complex. – Need to create thread for it – The serviceHandler as well – Onstartcommand which will get the intent and then pass the information onto the servicehandler (via a messenger) so it off on it’s own thread. – You can also setup the IBinder as well.

10 Service code example. Basically, I copied the code from the developer site and then added my own to the handleMessage(Message) method. http://developer.android.com/guide/compone nts/services.html#ExtendingService http://developer.android.com/guide/compone nts/services.html#ExtendingService – You may need to initialize some things in the OnCreate() and/or OnStartCommand(…) as well.

11 ServiceDemo Remember a service, is just like an activity, except there is no screen. – So most of the code is downloading files and using file I/O and the notification system. Nothing really amazing at this point. – When you need a service, you start it with an intent and the startService(intent) command. Just like you would an activity. The service end when they are done and what for the next call.

12 Binder Android Interface Definition Language (AIDL) – similar to other IDLs if you have worked with them before. It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).

13 Binder (2) In it’s simplest form, you allow the activity to call methods in the running service via the binder service. – Instead of startService BindService(intent, ServiceConnection, flags) Now you use the ServiceConnection variable to call into the service to retrieve data or cause the service to do something. – Hard to show in powerpoint, since there are lots of little pieces. Easier to show in code.

14 Manifest file. Like everything else services must be registered listed in the AndroidManifest.xml – Uses the tag and is pretty simple. – Example: <service android:name=".myIntentService“ android:enabled="true" android:exported="true">

15 Example code. The ServiceDemo – One example intent service for random numbers – One Example Service for random numbers You can compare the complexity of a service with an intentService – fileDlService is an intentService. It takes a URL (http://...) to down a picture and stores in the SD card downloads directory. When completed it sends a notification, so the user can open the file in the gallery viewer.

16 Example code (2) ServiceDemoIPC – Uses a ServiceConnection to allow the activity to call into the service to get a random number – Based on Googles code. ServiceDemoMSG – Setups a handler to send messages to a service This service will toast a message. But you can easily change that to have the service do many difference things, based on the message.

17 References http://developer.android.com/reference/android /app/Service.html http://developer.android.com/reference/android /app/Service.html http://developer.android.com/guide/components /services.html http://developer.android.com/guide/components /services.html http://www.vogella.com/articles/AndroidServices /article.html http://www.vogella.com/articles/AndroidServices /article.html http://developer.android.com/guide/components /aidl.html http://developer.android.com/guide/components /aidl.html http://developer.android.com/guide/components /bound-services.html http://developer.android.com/guide/components /bound-services.html

18 Q A &


Download ppt "Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what."

Similar presentations


Ads by Google