简体   繁体   中英

Background service not work Xamarin.Android

I have problem. My service worked, but when i close application service stoped. How i can leave my service is running?

service

[Service]
public class NotificationService : Service
{
  public NotificationService () { }

  public override StartCommandResult OnStartCommand (Android.Content.Intent intent, StartCommandFlags flags, int startId)
  {
    new Task(() =>
        {
            DoWork();
        } ).Start();
    return StartCommandResult.Sticky;
  }

public override void OnDestroy ()
{
    base.OnDestroy ();
}

  public override IBinder OnBind (Intent intent)
  {
    throw new NotImplementedException ();
  }

  void DoWork()
  {
    new Task(() =>
        {
            for (int i = 0; i < 100; i ++)
            {
                System.Threading.Thread.Sleep(1000);
                var context = Android.App.Application.Context;
                var builder = new Android.App.Notification.Builder(context)
                    .SetSmallIcon(Resource.Drawable.icon)
                    .SetContentTitle("My application")
                    .SetDefaults(NotificationDefaults.Sound)
                    .SetContentText(i.ToString())
                    .SetOngoing(true);
                var not = builder.Build();
                var notManager = context.GetSystemService(NotificationService) as NotificationManager;
                notManager.Notify(1, not);
            }
        }).Start();
    }
}

MainActivity.cs

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    global::Xamarin.Forms.Forms.Init (this, bundle);

    LoadApplication (new App ());

    new Task(() =>
        {
            var notificationIntent = new Intent(this, typeof(NotificationService));
            StartService(notificationIntent);
        }).Start(); 
    Android.Widget.Toast.MakeText(this, "run", Android.Widget.ToastLength.Short).Show();
}

When we start application from VS and stop an application. VS automatically close all services. Need build the app to the device and than start application from device.

Sorry for the late answer but I think it can help others. I would like to make something clear for you that you are writing this service as a background service. Background service can not run for long. There are some limitation of background services in Android after Android 8.0 onwards. Android automatically kills background service of an app after some time.

See this https://developer.android.com/about/versions/oreo/background

If you want to run a service for a long time then make the service Foreground Service . Please follow https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services for detailed knowledge of foreground service in Xamarin Forms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM