Your SlideShare is downloading. ×
0
Android Services in C# Developing Services with Mono for Android             October 4, 2012             Copyright 2012 © ...
Mike Bluestein                Technical Writer                Xamarin Documentation Team                mike.bluestein@xam...
AGENDA• Overview    of Mono for Android• Introduction   to Android Services• Started   Services• Bound   Services• Android...
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     ...
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   ...
STARTED SERVICES• Lifecycle        separate from starting component• Runs   beyond lifetime of caller• Performlong-running...
SERVICE CLASS• Base   class for services• Override    lifecycle methods• Register        with                      [Servic...
STARTING THE SERVICE• Context    subclass (Activity) calls StartService• Results   in OnStartCommand being called• Service...
THREADING• Service   runs in main thread• Code   in service lifecycle methods would block main thread  • Making   UI unres...
INTENT FILTERS• To   call a service in a local or remote scenario• Decorate     Service class with IntentFilterAttribute  ...
NOTIFICATIONS• Can    use Notifications to                               var nMgr = (NotificationManager)GetSystemService  ...
INTENTSERVICE CLASS• Simplifies   service development• Only    need to implement OnHandleIntent• Processes   requests seria...
INTENTSERVICE[Service][IntentFilter(new String[]{"com.xamarin.MyIntentService"})]public class MyIntentService: IntentServi...
BOUND SERVICES• Provide   a client-server interface• Can   be local or remote • Localfor in-app background   worker • Remo...
BOUND SERVICES• Created     when first client connects• Destroyed      when last client disconnects• If   also started serv...
CREATING A BOUND SERVICE• Subclass   the Binder class   public class MyServiceBinder : Binder                             ...
CALLING BOUND SERVICE• Client   calls BindService    var myServiceIntent = new Intent ("com.xamarin.MyService");          ...
SERVICE CONNECTION• IServiceConnection            class MyServiceConnection : Java.Lang.Object,                           ...
MESSENGER CLASS• Used   for calling services across process boundaries• Uses AIDL    internally• Create   a class inherits...
MESSENGER CLIENT• Implement    an IServiceConnection that creates a Messenger• Create   a Message object and add data to i...
SERVICE EXAMPLE
Xamarin    Seminar   Please give us your feedback  http://bit.ly/xamfeedback      Follow us on Twitter        @XamarinHQ  ...
Upcoming SlideShare
Loading in...5
×

Android Services in C# Seminar

22,433

Published on

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.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
22,433
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
84
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \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
  • Transcript of "Android Services in C# Seminar"

    1. 1. Android Services in C# Developing Services with Mono for Android October 4, 2012 Copyright 2012 © Xamarin Inc. All rights reserved
    2. 2. Mike Bluestein Technical Writer Xamarin Documentation Team mike.bluestein@xamarin.com @mikebluestein XamarinCopyright 2012 © Xamarin Inc. All rights reserved
    3. 3. AGENDA• Overview of Mono for Android• Introduction to Android Services• Started Services• Bound Services• Android Service Example
    4. 4. MONO FOR ANDROID Android Applications in C#
    5. 5. OS SUPPORT• OSX • MonoDevelop• Windows 7 (Windows 8) • Visual Studio 2010 (2012) Pro or above • MonoDevelop
    6. 6. ANDROID SDK• Intents • Bindings Android SDK• Activities • Mono.Android.dll• Content Providers • Code Reuse• Fragments • iOS• Services • Windows Phone• etc ...
    7. 7. SERVICES• Allow background processing• Service lifecycle independent of Activity lifecycle
    8. 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. 9. STARTED SERVICES• Lifecycle separate from starting component• Runs beyond lifetime of caller• Performlong-running background work
    10. 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. 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. 12. THREADING• Service runs in main thread• Code in service lifecycle methods would block main thread • Making UI unresponsive• Use System.Threading
    13. 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. 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. 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
    16. 16. 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 }}
    17. 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. 18. BOUND SERVICES• Created when first client connects• Destroyed when last client disconnects• If also started service, both life-cycles apply
    19. 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. 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. 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. 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. 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
    24. 24. SERVICE EXAMPLE
    25. 25. Xamarin Seminar Please give us your feedback http://bit.ly/xamfeedback Follow us on Twitter @XamarinHQ Copyright 2012 © Xamarin Inc. All rights reserved
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×