Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

AAR Binding + intent to start service

SteeveSteeve Member ✭✭

Hello the community :)

I'm trying to implement the binding of an external Android library into my projet.
The aim is to capture events sent from a laser barcode reader.

Documentation is here :
https://help.famoco.com/developers/devices/PX320/px320-imager/

I've created my binding library, I've created my broadcast receiver to capture intents sent from the device but I can't find the way to send the intent to initialize the service.
Can you please help me to find how to translate this to Xamarin Forms code please ?

Intent intent = new Intent("com.zebra.sdl.action.START"); intent.setPackage("com.zebra.sdl"); intent.setFlags(Intent.FLAG_FROM_BACKGROUND); startService(intent);

Thanks

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    You can create a dependenceService to execute the above Intent code.

    Please create an interface in the PCL

       public interface IOpenService
        {
           void StartMyService();
        }
    

    Then achieve the Interface in the android platform.

    [assembly: Dependency(typeof(OpenService))]
    namespace App32.Droid
    {
        class OpenService : IOpenService
        {
            public void StartMyService()
            {
    
    
                Intent intent = new Intent("com.zebra.sdl.action.START"); 
                intent.SetPackage("com.zebra.sdl"); 
                intent.SetFlags(ActivityFlags.FromBackground);
                Android.App.Application.Context.StartService(intent);
    
            }
        }
    }
    

    When you want to execute it in the PCL, you can run following code.

           private void Button_Clicked(object sender, EventArgs e)
            {
                DependencyService.Get<IOpenService>().StartMyService();
            }
    
Sign In or Register to comment.