Video for this session: http://www.youtube.com/watch?v=tqB4jss5SOQ
Code for this session: https://github.com/xamarin/Seminars/tree/master/2012-10-04-AndroidServices
This session discusses how to develop services in C# using Mono for Android. Mike covers the basics of how Android services work and examines the various scenarios that services are used when developing Android applications.
7. SERVICES
• Allow background processing
• Service lifecycle independent of Activity lifecycle
8. SERVICES
• Two ways services can be used
• Started Services - perform
long running task
• Bounds Services - remote
interface for callers
• Same service can be both
started and bound
figure from developer.android.com
9. STARTED SERVICES
• Lifecycle
separate from starting
component
• Runs beyond lifetime of caller
• Performlong-running
background work
10. SERVICE CLASS
• Base class for services
• Override lifecycle methods
• Register
with [Service]
public class MyService : Service
AndroidManifest.xml
• ServiceAttribute
in <service android:name="myservice.MyService"/>
Mono for Android
11. STARTING THE SERVICE
• Context subclass (Activity) calls StartService
• Results in OnStartCommand being called
• Service stopped by calling Context.StopService or StopSelf
StopService (new Intent (this, typeof(MyService)));
• Also StopSelfResult to stop using startId
• Prevents premature stops when multiple callers
12. THREADING
• Service runs in main thread
• Code in service lifecycle methods would block main thread
• Making UI unresponsive
• Use System.Threading
13. INTENT FILTERS
• To call a service in a local or remote scenario
• Decorate Service class with IntentFilterAttribute
[Service]
[IntentFilter(new String[]{"com.xamarin.MyService"})]
public class MyService : Service
• Call using action from IntentFilter
StartService (new Intent ("com.xamarin.MyService"));
14. NOTIFICATIONS
• Can
use Notifications to
var nMgr = (NotificationManager)GetSystemService
(NotificationService);
communicate to user var notification = new Notification
(Resource.Drawable.Icon, "Message from
service");
• For example, to let user var pendingIntent = PendingIntent.GetActivity
(this, 0, new Intent (this, typeof(MyActivity)),
0);
know long running task notification.SetLatestEventInfo (this, "My
has completed Service Notification", "Message from service",
pendingIntent);
nMgr.Notify (0, notification);
• Required for services
started in the foreground
15. INTENTSERVICE CLASS
• Simplifies service development
• Only need to implement OnHandleIntent
• Processes requests serially using worker queue
• Worker processes each intent on separate thread
• Stops itself internally by calling StopSelf
17. BOUND SERVICES
• Provide a client-server interface
• Can be local or remote
• Localfor in-app background
worker
• Remote for calling across
process boundaries
18. BOUND SERVICES
• Created when first client connects
• Destroyed when last client disconnects
• If also started service, both life-cycles apply
19. CREATING A BOUND SERVICE
• Subclass the Binder class public class MyServiceBinder : Binder
{
MyService service;
public MyServiceBinder (MyService service)
• Implement OnBind {
this.service = service;
}
• Returnan instance of the public MyService GetMyService ()
{
return service;
Binder subclass }
}
public override IBinder OnBind (Intent intent)
{
binder = new MyServiceBinder (this);
return binder;
}
20. CALLING BOUND SERVICE
• Client calls BindService var myServiceIntent = new Intent ("com.xamarin.MyService");
myServiceConnection = new MyServiceConnection (this);
BindService (myServiceIntent, myServiceConnection,
• Intent Bind.AutoCreate);
• ServiceConnection
• UnbindService to unbind
21. SERVICE CONNECTION
• IServiceConnection class MyServiceConnection : Java.Lang.Object,
IServiceConnection
{
MyServiceBinder binder;
• OnServiceConnected public void OnServiceConnected (ComponentName
name, IBinder service)
{
binder = service as MyServiceBinder;
• Getreference to binder used }
...
to obtain service interface }
22. MESSENGER CLASS
• Used for calling services across process boundaries
• Uses AIDL internally
• Create a class inherits from Handler
• This will handle incoming messages
• In
service implementation create Messenger, passing it the
Handler
23. MESSENGER CLIENT
• Implement an IServiceConnection that creates a Messenger
• Create a Message object and add data to it
• Call the Send method of the Messenger