150 likes | 250 Views
Cosc 5/4730. Android Services. What is a service?. From android developer web pages: Most confusion about the Service class actually revolves around what it is not :
E N D
Cosc 5/4730 Android Services
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. • 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. http://developer.android.com/reference/android/app/Service.html
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.
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).
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.
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!
IntentService Example public class myIntentServiceextends 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. } }
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.
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.
IBinder • 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). We won’t be covering this in class. You’ll need to look that one up on your own.
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/components/services.html#ExtendingService • You may need to intialize some things in the OnCreate() and/or OnStartCommand(…) as well.
Manifest file. • Like everything else services must be registered listed in the AndroidManifest.xml • Uses the <service> tag and is pretty simple. • Example: <service android:name=".myIntentService“ android:label="@string/app_name" > </service> • I don’t think the label is required, but most examples show it, so I used as well.
Example code. • The ServiceDemo.zip • One example intent service for random numbers • One Example Service for random numbers • You can compare the complexity of a service with an intentService • fileDlServiceis 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.
References • http://developer.android.com/reference/android/app/Service.html • http://developer.android.com/guide/components/services.html • http://www.vogella.com/articles/AndroidServices/article.html • http://developer.android.com/guide/components/aidl.html
Q A &