SlideShare une entreprise Scribd logo
1  sur  25
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




                                                    Xamarin
Copyright 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

Contenu connexe

Plus de Xamarin

Creative Hacking: Delivering React Native App A/B Testing Using CodePush
Creative Hacking: Delivering React Native App A/B Testing Using CodePushCreative Hacking: Delivering React Native App A/B Testing Using CodePush
Creative Hacking: Delivering React Native App A/B Testing Using CodePushXamarin
 
Build Better Games with Unity and Microsoft Azure
Build Better Games with Unity and Microsoft AzureBuild Better Games with Unity and Microsoft Azure
Build Better Games with Unity and Microsoft AzureXamarin
 
Exploring UrhoSharp 3D with Xamarin Workbooks
Exploring UrhoSharp 3D with Xamarin WorkbooksExploring UrhoSharp 3D with Xamarin Workbooks
Exploring UrhoSharp 3D with Xamarin WorkbooksXamarin
 
Desktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for XamarinDesktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for XamarinXamarin
 
Developer’s Intro to Azure Machine Learning
Developer’s Intro to Azure Machine LearningDeveloper’s Intro to Azure Machine Learning
Developer’s Intro to Azure Machine LearningXamarin
 
Customizing Xamarin.Forms UI
Customizing Xamarin.Forms UICustomizing Xamarin.Forms UI
Customizing Xamarin.Forms UIXamarin
 
Session 4 - Xamarin Partner Program, Events and Resources
Session 4 - Xamarin Partner Program, Events and ResourcesSession 4 - Xamarin Partner Program, Events and Resources
Session 4 - Xamarin Partner Program, Events and ResourcesXamarin
 
Session 3 - Driving Mobile Growth and Profitability
Session 3 - Driving Mobile Growth and ProfitabilitySession 3 - Driving Mobile Growth and Profitability
Session 3 - Driving Mobile Growth and ProfitabilityXamarin
 
Session 2 - Emerging Technologies in your Mobile Practice
Session 2 - Emerging Technologies in your Mobile PracticeSession 2 - Emerging Technologies in your Mobile Practice
Session 2 - Emerging Technologies in your Mobile PracticeXamarin
 
Session 1 - Transformative Opportunities in Mobile and Cloud
Session 1 - Transformative Opportunities in Mobile and Cloud Session 1 - Transformative Opportunities in Mobile and Cloud
Session 1 - Transformative Opportunities in Mobile and Cloud Xamarin
 
SkiaSharp Graphics for Xamarin.Forms
SkiaSharp Graphics for Xamarin.FormsSkiaSharp Graphics for Xamarin.Forms
SkiaSharp Graphics for Xamarin.FormsXamarin
 
Building Games for iOS, macOS, and tvOS with Visual Studio and Azure
Building Games for iOS, macOS, and tvOS with Visual Studio and AzureBuilding Games for iOS, macOS, and tvOS with Visual Studio and Azure
Building Games for iOS, macOS, and tvOS with Visual Studio and AzureXamarin
 
Intro to Xamarin.Forms for Visual Studio 2017
Intro to Xamarin.Forms for Visual Studio 2017Intro to Xamarin.Forms for Visual Studio 2017
Intro to Xamarin.Forms for Visual Studio 2017Xamarin
 
Connected Mobile Apps with Microsoft Azure
Connected Mobile Apps with Microsoft AzureConnected Mobile Apps with Microsoft Azure
Connected Mobile Apps with Microsoft AzureXamarin
 
Introduction to Xamarin for Visual Studio 2017
Introduction to Xamarin for Visual Studio 2017Introduction to Xamarin for Visual Studio 2017
Introduction to Xamarin for Visual Studio 2017Xamarin
 
Building Your First iOS App with Xamarin for Visual Studio
Building Your First iOS App with Xamarin for Visual StudioBuilding Your First iOS App with Xamarin for Visual Studio
Building Your First iOS App with Xamarin for Visual StudioXamarin
 
Building Your First Android App with Xamarin
Building Your First Android App with XamarinBuilding Your First Android App with Xamarin
Building Your First Android App with XamarinXamarin
 
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#Xamarin
 
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOpsXamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOpsXamarin
 
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...Xamarin
 

Plus de Xamarin (20)

Creative Hacking: Delivering React Native App A/B Testing Using CodePush
Creative Hacking: Delivering React Native App A/B Testing Using CodePushCreative Hacking: Delivering React Native App A/B Testing Using CodePush
Creative Hacking: Delivering React Native App A/B Testing Using CodePush
 
Build Better Games with Unity and Microsoft Azure
Build Better Games with Unity and Microsoft AzureBuild Better Games with Unity and Microsoft Azure
Build Better Games with Unity and Microsoft Azure
 
Exploring UrhoSharp 3D with Xamarin Workbooks
Exploring UrhoSharp 3D with Xamarin WorkbooksExploring UrhoSharp 3D with Xamarin Workbooks
Exploring UrhoSharp 3D with Xamarin Workbooks
 
Desktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for XamarinDesktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
 
Developer’s Intro to Azure Machine Learning
Developer’s Intro to Azure Machine LearningDeveloper’s Intro to Azure Machine Learning
Developer’s Intro to Azure Machine Learning
 
Customizing Xamarin.Forms UI
Customizing Xamarin.Forms UICustomizing Xamarin.Forms UI
Customizing Xamarin.Forms UI
 
Session 4 - Xamarin Partner Program, Events and Resources
Session 4 - Xamarin Partner Program, Events and ResourcesSession 4 - Xamarin Partner Program, Events and Resources
Session 4 - Xamarin Partner Program, Events and Resources
 
Session 3 - Driving Mobile Growth and Profitability
Session 3 - Driving Mobile Growth and ProfitabilitySession 3 - Driving Mobile Growth and Profitability
Session 3 - Driving Mobile Growth and Profitability
 
Session 2 - Emerging Technologies in your Mobile Practice
Session 2 - Emerging Technologies in your Mobile PracticeSession 2 - Emerging Technologies in your Mobile Practice
Session 2 - Emerging Technologies in your Mobile Practice
 
Session 1 - Transformative Opportunities in Mobile and Cloud
Session 1 - Transformative Opportunities in Mobile and Cloud Session 1 - Transformative Opportunities in Mobile and Cloud
Session 1 - Transformative Opportunities in Mobile and Cloud
 
SkiaSharp Graphics for Xamarin.Forms
SkiaSharp Graphics for Xamarin.FormsSkiaSharp Graphics for Xamarin.Forms
SkiaSharp Graphics for Xamarin.Forms
 
Building Games for iOS, macOS, and tvOS with Visual Studio and Azure
Building Games for iOS, macOS, and tvOS with Visual Studio and AzureBuilding Games for iOS, macOS, and tvOS with Visual Studio and Azure
Building Games for iOS, macOS, and tvOS with Visual Studio and Azure
 
Intro to Xamarin.Forms for Visual Studio 2017
Intro to Xamarin.Forms for Visual Studio 2017Intro to Xamarin.Forms for Visual Studio 2017
Intro to Xamarin.Forms for Visual Studio 2017
 
Connected Mobile Apps with Microsoft Azure
Connected Mobile Apps with Microsoft AzureConnected Mobile Apps with Microsoft Azure
Connected Mobile Apps with Microsoft Azure
 
Introduction to Xamarin for Visual Studio 2017
Introduction to Xamarin for Visual Studio 2017Introduction to Xamarin for Visual Studio 2017
Introduction to Xamarin for Visual Studio 2017
 
Building Your First iOS App with Xamarin for Visual Studio
Building Your First iOS App with Xamarin for Visual StudioBuilding Your First iOS App with Xamarin for Visual Studio
Building Your First iOS App with Xamarin for Visual Studio
 
Building Your First Android App with Xamarin
Building Your First Android App with XamarinBuilding Your First Android App with Xamarin
Building Your First Android App with Xamarin
 
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
 
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOpsXamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
 
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...
 

Dernier

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 

Dernier (20)

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 

Android Services in C# Seminar

  • 1. Android Services in C# Developing Services with Mono for Android October 4, 2012 Copyright 2012 © Xamarin Inc. All rights reserved
  • 2. Mike Bluestein Technical Writer Xamarin Documentation Team mike.bluestein@xamarin.com @mikebluestein Xamarin Copyright 2012 © Xamarin Inc. All rights reserved
  • 3. AGENDA • Overview of Mono for Android • Introduction to Android Services • Started Services • Bound Services • Android Service Example
  • 4. MONO FOR ANDROID Android Applications in C#
  • 5. OS SUPPORT • OSX • MonoDevelop • Windows 7 (Windows 8) • Visual Studio 2010 (2012) Pro or above • MonoDevelop
  • 6. ANDROID SDK • Intents • Bindings Android SDK • Activities • Mono.Android.dll • Content Providers • Code Reuse • Fragments • iOS • Services • Windows Phone • etc ...
  • 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
  • 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. 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
  • 25. Xamarin Seminar Please give us your feedback http://bit.ly/xamfeedback Follow us on Twitter @XamarinHQ Copyright 2012 © Xamarin Inc. All rights reserved

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n