启动然后销毁前景服务不会停止VS调试器

时间:2019-12-03 20:40:28

标签: android xamarin foreground-service

我有一个运行良好的前台服务,当我关闭应用程序时,我使用OnDestroy方法停止了我的服务。一切似乎都正常,但是我在VS2019中的调试器从未停止。我在模拟器中的“开发人员”部分检查了“正在运行的服务”,但没有一个(与此项目有关),该应用程序已关闭。

这使我相信后面可能仍然有东西在运行-或在这种情况下VS会出现问题。我是否以错误的方式进行启动和停止?

这是我的服务

[Service]
public class ForegroundService : Service
{
    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    public override void OnDestroy()
    {
        if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
        {
            StopForeground(StopForegroundFlags.Remove);
        }
        else
        {
            StopForeground(true);
        }
        StopSelf();

        base.OnDestroy();
    }

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        PendingIntent pendingIntent = PendingIntent.GetActivity(
            Android.App.Application.Context,
            NotificationSettings.NotificationPendingIntentId,
            new Intent(Android.App.Application.Context, typeof(MainActivity)),
            PendingIntentFlags.UpdateCurrent);

        var notification = new NotificationCompat.Builder(Android.App.Application.Context, NotificationSettings.ChannelId)
            .SetContentIntent(pendingIntent)
            .SetContentTitle(NotificationSettings.Notification_app_name)
            .SetContentText(NotificationSettings.Notification_text)
            .SetLargeIcon(BitmapFactory.DecodeResource(Android.App.Application.Context.Resources, Resource.Drawable.xamagonBlue))
            .SetSmallIcon(Resource.Drawable.xamagonBlue)
            .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate)
            .Build();

        StartForeground(NotificationSettings.SERVICE_RUNNING_NOTIFICATION_ID, notification);

        return StartCommandResult.Sticky;
    }
}

这是在MainActivity内部:

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;
    base.OnCreate(savedInstanceState);
    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    CrossCurrentActivity.Current.Init(this, savedInstanceState);
    LoadApplication(new App());

    CreateNotificationChannel();

    startServiceIntent = new Intent(this, typeof(ForegroundService));
    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
    {
        StartForegroundService(intent);
    }
    else
    {
        StartService(intent);
    }
}

protected override void OnDestroy()
{
    StopService(startServiceIntent);

    base.OnDestroy();
}

更新1

我认为我正在做某事。如果我在按钮上StopService(startServiceIntent);单击,然后在模拟器中关闭应用程序,则调试器将正确停止。我的想法是OnDestroy来不及调用StopService太晚了,这引发了另一个问题,我何时应该停止服务?

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,不得不在服务类中使用它

public override void OnTaskRemoved(Intent rootIntent)
{   
     if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
         StopForeground(true);
     else
         StopService(new Intent(this, typeof(YourServiceClass))); 
    base.OnTaskRemoved(rootIntent);
    //will kill threads when app is manually killed by user.
    Java.Lang.JavaSystem.Exit(0);
}