Android Services in C# Seminar
Upcoming SlideShare
Loading in...5

Android Services in C# Seminar



Video for this session: ...

Video for this session:

Code for this session:

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.



Total Views
Views on SlideShare
Embed Views



14 Embeds 18,268 11504 5655 939 71 57 20 8 5
http://localhost 3 2 HTTP 1 1 1 1



Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Android Services in C# Seminar Android Services in C# Seminar Presentation Transcript

  • Android Services in C# Developing Services with Mono for Android October 4, 2012 Copyright 2012 © Xamarin Inc. All rights reserved
  • Mike Bluestein Technical Writer Xamarin Documentation Team @mikebluestein XamarinCopyright 2012 © Xamarin Inc. All rights reserved
  • AGENDA• Overview of Mono for Android• Introduction to Android Services• Started Services• Bound Services• Android Service Example
  • MONO FOR ANDROID Android Applications in C#
  • OS SUPPORT• OSX • MonoDevelop• Windows 7 (Windows 8) • Visual Studio 2010 (2012) Pro or above • MonoDevelop
  • ANDROID SDK• Intents • Bindings Android SDK• Activities • Mono.Android.dll• Content Providers • Code Reuse• Fragments • iOS• Services • Windows Phone• etc ...
  • SERVICES• Allow background processing• Service lifecycle independent of Activity lifecycle
  • 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
  • STARTED SERVICES• Lifecycle separate from starting component• Runs beyond lifetime of caller• Performlong-running background work
  • 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
  • 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
  • THREADING• Service runs in main thread• Code in service lifecycle methods would block main thread • Making UI unresponsive• Use System.Threading
  • 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"));
  • 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
  • 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
  • INTENTSERVICE[Service][IntentFilter(new String[]{"com.xamarin.MyIntentService"})]public class MyIntentService: IntentService{ ... protected override void OnHandleIntent (Android.Content.Intent intent) { // do long running work here }}
  • BOUND SERVICES• Provide a client-server interface• Can be local or remote • Localfor in-app background worker • Remote for calling across process boundaries
  • BOUND SERVICES• Created when first client connects• Destroyed when last client disconnects• If also started service, both life-cycles apply
  • 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; }
  • 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
  • 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 }
  • 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
  • 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
  • Xamarin Seminar Please give us your feedback Follow us on Twitter @XamarinHQ Copyright 2012 © Xamarin Inc. All rights reserved