• Like
  • Save
Android Services in C# Seminar
Upcoming SlideShare
Loading in...5
×
 

Android Services in C# Seminar

on

  • 22,146 views

Video for this session: http://www.youtube.com/watch?v=tqB4jss5SOQ ...

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.

Statistics

Views

Total Views
22,146
Views on SlideShare
3,889
Embed Views
18,257

Actions

Likes
1
Downloads
65
Comments
0

14 Embeds 18,257

http://blog.xamarin.com 11493
http://www.go-mono.com 5655
http://go-mono.com 939
http://www.go-mono.org 71
http://go-mono.org 57
http://go-mono.net 20
http://translate.googleusercontent.com 8
http://webcache.googleusercontent.com 5
http://localhost 3
http://www.hanrss.com 2
http://www.diffbot.com&_=1350339007509 HTTP 1
http://127.0.0.1 1
http://cc.bingj.com 1
http://plus.url.google.com 1
More...

Accessibility

Categories

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.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
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 mike.bluestein@xamarin.com @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 developer.android.com
  • 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
  • SERVICE EXAMPLE
  • Xamarin Seminar Please give us your feedback http://bit.ly/xamfeedback Follow us on Twitter @XamarinHQ Copyright 2012 © Xamarin Inc. All rights reserved