Android 启动并销毁前台服务赢得';t停止VS调试器
我有一个运行良好的前台服务,当我关闭应用程序时,我使用OnDestroy方法停止我的服务。一切似乎都正常,但我在VS2019中的调试器从未停止过。我检查了emulator中运行服务的developer部分,发现没有(与此项目相关的),应用程序关闭 这使我相信,可能仍然有一些东西在后面运行-或者VS在这个场景中不知何故被窃听了。我是否以错误的方式启动和停止 这是我的服务Android 启动并销毁前台服务赢得';t停止VS调试器,android,xamarin,foreground-service,Android,Xamarin,Foreground Service,我有一个运行良好的前台服务,当我关闭应用程序时,我使用OnDestroy方法停止我的服务。一切似乎都正常,但我在VS2019中的调试器从未停止过。我检查了emulator中运行服务的developer部分,发现没有(与此项目相关的),应用程序关闭 这使我相信,可能仍然有一些东西在后面运行-或者VS在这个场景中不知何故被窃听了。我是否以错误的方式启动和停止 这是我的服务 [Service] public class ForegroundService : Service { public
[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;
}
}
这是一项主要活动:
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
我想我明白了。如果I
StopService(启动服务意图)代码>单击按钮,然后关闭emulator中的应用程序,调试器将正确停止。我的想法是OnDestroy调用StopService已经太迟了,这导致了另一个问题,我应该在什么时候停止我的服务?我也遇到了同样的问题,不得不在我的服务类中使用它
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);
}
马克:谢谢,这很有效。根本不需要使用OnDestroy,OnTaskRemoved就足够了。我很高兴它对您有所帮助!如果用户在不关闭应用程序的情况下取消通知,OnDestroy仍将被调用,因此如果需要,您可以使用它释放资源!