SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

Más 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
 

Más 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...
 

Último

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 

Último (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 

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

Notas del editor

  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