0

My problem is the following . I have created android service which is winning even after the application is closed. When the application is active i can scan for the service and i can see it running and i can stop it. When i close the activity completely and start the app I again scan for active services i cannot find it in the list. So how to stop the service after the restart of the program so i can get indication if service is still running in the background. I use the bellow method for checking the service.

public void IsServiceRunning()
    {
        ActivityManager activityManager = (ActivityManager)GetSystemService(Context.ActivityService);
        var list = activityManager.GetRunningServices(int.MaxValue);

        foreach(var item in list)
        {

            Toast.MakeText(ApplicationContext, item.Service.ClassName + " " + item.Started + " " + item.Service.Class, ToastLength.Short).Show();
            Log.Debug("++++++++++++++++++++++++++++++++++ ",  item.Service.ClassName + " " + item.Started + " " + item.Service.Class);
            if ( item.Service.ClassName.Contains("MyService"))
            {
                MessagingCenter.Send<string>("true", "CheckRunningService");
                //Toast.MakeText(ApplicationContext, item.Service.ShortClassName.ToString(), ToastLength.Short).Show();
                return;
            }



        }


        MessagingCenter.Send<string>("false", "CheckRunningService");

    }

Service Code

class MyService : Service 
{

    SmsBroadcastRceiver sms;
    public override void OnCreate()
    {
        base.OnCreate();

    }

    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {

        //Toast.MakeText(this, "MyService Started", ToastLength.Long).Show();
        sms = new SmsBroadcastRceiver();
        RegisterReceiver(sms, new IntentFilter("SMS_RECEIVED"));


        return StartCommandResult.NotSticky;

    }


    public override void OnDestroy()
    {
        UnregisterReceiver(sms);
        StopSelf();

        //Toast.MakeText(this, "MyService Stopped", ToastLength.Long).Show();

        base.OnDestroy();

    }

    public override IBinder OnBind(Intent intent)
    {

        return null;
    }


}
Evgeni Tanchev
  • 93
  • 1
  • 11

2 Answers2

0

Try to put android:stopWithTask​=​"true" with service,which is declared in manifest file and In the service class Override the onTaskRemoved method and check also you can remove updates of location listener here to stop the location updates

Hope it will help for you

gpuser
  • 1,143
  • 1
  • 9
  • 6
  • according to "Android Services - Unable to stop after application close " line of question just put this in manifest file – gpuser Nov 11 '19 at 15:12
  • Please see here about service in API level 26 https://stackoverflow.com/a/58410396/4042384 hope it helps you – gpuser Nov 11 '19 at 15:18
0

I think i found why once my app is stopped i cannot find it. As stated bellow from the documentation :

getRunningServices(int maxNum) This method was deprecated in API level 26. As of Build.VERSION_CODES.O, this method is no longer available to third party applications. For backwards compatibility, it will still return the caller's own services.

Evgeni Tanchev
  • 93
  • 1
  • 11