Cosc 5/4730
AndroidServices
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, or
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)
• Starting a service– You app can start a service with startService call. The
service will then run in the background– You are responsible for stopping it
• Either stopself() (in the service) or stopService in the app.
• Binding to a service– You app is bound to the service (and it’s started), normally
with a RPC, with bindService• The service returns a IBinder method, which allows the app
communicate directly with the service.• If you service won’t allow binding, then the IBinder returns null.
What is a service? (5)• 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 Examplepublic class myIntentService extends IntentService {• required constructorpublic 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.
}}
See the myIntentService.java for the compete code. In the service repo, serviceDemo example
Calling the IntentService• From the application we create an intent and then start the
service.– The example service “returns” X number of random numbers based on
the intent, soIntent number5 = new Intent(getBaseContext(), myIntentService.class);
number5.putExtra("times", 5); //5 random number– And we want it to send numbers back through a messenger (handler)
Messenger messenger = new Messenger(handler);number5.putExtra("MESSENGER", messenger);• As note, If there is no MESSENGER key, then the service will use notifications.
– And finally start the service, using the intent that was createdstartService(number5);
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.
– Well look at the template for the service code next.
Service codepublic class myService extends Service { private Looper mServiceLooper; private ServiceHandler mServiceHandler;
//our global variables here.
// Handler that receives messages from the thread private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); }
@Override public void handleMessage(Message msg) { //our code here, to the work based on the msg.
} // Stop the service using the startId, so that we don't stop // the service in the middle of handling another job stopSelf(msg.arg1); } }
@Override public void onCreate() {// Start up the thread running the service. Note that // we create a separate thread because the service// normally runs in the process's main thread, which we// don't want to block. We also make it// background priority so CPU-intensive work will not// disrupt our UI.
HandlerThread thread = new HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND); thread.start();
// Get the HandlerThread's Looper and use it for our Handler mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); }
Service code (2) @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
// For each start request, send a message to start a job and deliver the // start ID so we know which request we're stopping when we finish the job Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; mServiceHandler.sendMessage(msg);
// If we get killed, don’t restart, wait for a startservice() to startup again. return START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { // We don't provide binding, so return null return null; }
@Override public void onDestroy() { Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); }}
Service code (3)
• In the AppIntent intent = new Intent(this, myService.class);startService(intent);– The startService() method returns immediately
and the Android system calls the service's onStartCommand() method.
– If the service is not already running, the system first calls onCreate(), then calls onStartCommand().
Service code example.
• I’m using the template code from before and adding to it where listed in red text.– I copied the code from the developer site– http://
developer.android.com/guide/components/services.html#ExtendingService
– You may need to initialize some things in the OnCreate() and/or OnStartCommand(…) as well.
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 ends when they are done and what for the next call.
AIDL
• 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 are not covering AIDL, in this class
• http://developer.android.com/guide/components/aidl.html
Bound Services
• A bound service is the server in a client-server interface. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC). – You need to implement the onBind() method and
return a IBinder an IBinder object.– The activity uses the object to “call into” the service.
• Or get a handler and send messages to the service.• You can also use the AIDL as well.
Binder
• 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.
Binder implementation• In your service, create an instance of Binder that either:
– contains public methods that the client can call– returns the current Service instance, which has public methods the client can
call– or, returns an instance of another class hosted by the service with public
methods the client can call• Return this instance of Binder from the onBind() callback method.• In the Activity/fragment/whatever, receive the Binder from the
onServiceConnected() callback method and make calls to the bound service using the methods provided.
– Lets look at the example code for see how the pieces go together• ServiceDemoIPC uses binder so the activity can use it’s methods• ServiceDemoMSG uses the binder to setup a handler to send messages to the service.
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:enabled="true" android:exported="true"></service>
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.
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.
References
• http://developer.android.com/reference/android/app/Service.html
• http://www.vogella.com/articles/AndroidServices/article.html
• http://developer.android.com/guide/components/services.html – http://developer.android.com/guide/components/aidl.html – http://
developer.android.com/guide/components/bound-services.html
QA&