Android service closes when I close the app that starts it

0

I'm trying to do a service that runs in the background when the app is closed, I share the code I'm using:

Namespace myapp.droid.mynamespace

Public class miservicio : service
{
    Private LocationManager lManager;

    Public Location GetLocation()
    {
        Location ubicacion = null
        lManager = (LocationManager)Forms.Context.GetSystemService(LocationService);
        If(lManager.AllProviders.Count <= 0) return null;
        Var provider = lManager.GetBestProvider(criteria, true);
        If(lManager.IsProviderEnabled(provider))
        {
            ubicacion = lManger.GetLastKnownLocation(provider);
        }

        Return ubicacion;
    }

    Public override StartCommandResult(Intent intent, StartCommandFlags flags, Int startId)
    {
        Var iniciado = false;
        Var t = new Task(() =>
        {
            Do
            {
                Iniciado = true;
                Ubicacion = GetLocation();

                If(ubicacion != null) mensaje = $"lat: {ubicacion.latitud} - long: {ubicacion.longitud}";
                Notificacion(mensaje);
                Thead.sleep(5000);
            }while(iniciado);
        });
        t.start();

        Return StartCommandResult.Sticky;
    }

    Public override IBinder OnBind(Intent intent)
    {
        Return null;
    }

    Public override OnTaskRrmoved(Intent intent)
    {
         Base.OnTaskRemoves(intent);
        Var intent2 = new Intent(this, typeof(miservicio));
        Var pIntent = PendingIntent.GetService(this, 1, PendingIntentFlags.OneShot);
    }
}

And in my activity I start it like this:

Void IniciarServicio()
{
    Var intent = new Intent(Application.Context, typeof(Miservicio));
    Application.Context.StartService(intent);
}

And I call it in the OnCreate of my activity, the service closes as soon as I close the app completely, has it happened to someone previously? Any help is welcome thanks!

    
asked by Frank Guerra 21.02.2018 в 00:11
source

0 answers