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;
}
}