Xamarin 当应用程序处于后台时,保持任务工作

Xamarin 当应用程序处于后台时,保持任务工作,xamarin,xamarin.forms,xamarin.android,xamarin.ios,background-process,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,Background Process,我正在Xamarin表单上编写一个地图跟踪应用程序。我正在使用异步任务(与Device.StartTimer一起)来跟踪位置和计数器计时器的另一个任务,但是当我需要打开另一个应用程序时,例如音乐应用程序或任何应用程序在后台时无法跟踪。当我在后台发送应用程序时,所有任务都将停止。当我再次启动应用程序时,任务不会继续。 我只需要在应用程序运行时继续工作 如何做到这一点?在这种情况下,您应该使用后台任务,它在应用程序的生命周期之外运行。您可以在此处找到更多信息在本例中,您应该使用后台任务,它在应用程序

我正在Xamarin表单上编写一个地图跟踪应用程序。我正在使用
异步任务(与Device.StartTimer一起)
来跟踪位置和计数器计时器的另一个任务,但是当我需要打开另一个应用程序时,例如音乐应用程序或任何应用程序在后台时无法跟踪。当我在后台发送应用程序时,所有任务都将停止。当我再次启动应用程序时,任务不会继续。 我只需要在应用程序运行时继续工作


如何做到这一点?

在这种情况下,您应该使用后台任务,它在应用程序的生命周期之外运行。您可以在此处找到更多信息

在本例中,您应该使用后台任务,它在应用程序的生命周期之外运行。您可以在此处找到更多信息

由于或稍后,正常服务将在后台终止

在Android 8.0或更高版本中,我建议您实现一个目标以实现这一目标(接收比“常规”服务更高的优先级,前台服务必须提供一个通知,只要服务运行,Android就会显示)

您可以使用dependence service以xamarin表单打开前台服务

IService.cs为android创建一个启动服务的界面

public interface IService
{
  void Start();
}
然后实现
DependentService
以启动前台服务

DependentService.cs

[assembly: Xamarin.Forms.Dependency(typeof(DependentService))]
namespace TabGuesture.Droid
{
  [Service]
public class DependentService : Service, IService
{
public void Start()
{
    var intent = new Intent(Android.App.Application.Context, 
    typeof(DependentService));


    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
    {
        Android.App.Application.Context.StartForegroundService(intent);
    }
    else
    {
        Android.App.Application.Context.StartService(intent);
    }
}

public override IBinder OnBind(Intent intent)
{
    return null;
}
public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;
public override StartCommandResult OnStartCommand(Intent intent, 
StartCommandFlags flags, int startId)
{
    // From shared code or in your PCL

    CreateNotificationChannel();
    string messageBody = "service starting";

    var notification = new Notification.Builder(this, "10111")
    .SetContentTitle(Resources.GetString(Resource.String.app_name))
    .SetContentText(messageBody)
    .SetSmallIcon(Resource.Drawable.main)
    .SetOngoing(true)
    .Build();
    StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
    //do you work
    return StartCommandResult.Sticky;
}


void CreateNotificationChannel()
{
    if (Build.VERSION.SdkInt < BuildVersionCodes.O)
    {
        // Notification channels are new in API 26 (and not a part of the
        // support library). There is no need to create a notification
        // channel on older versions of Android.
        return;
    }

    var channelName = Resources.GetString(Resource.String.channel_name);
    var channelDescription = GetString(Resource.String.channel_description);
    var channel = new NotificationChannel("10111", channelName, NotificationImportance.Default)
    {
        Description = channelDescription
    };

    var notificationManager = (NotificationManager)GetSystemService(NotificationService);
    notificationManager.CreateNotificationChannel(channel);
 }
}
}
[assembly:Xamarin.Forms.Dependency(typeof(DependentService))]
名称空间TabGuesture.Droid
{
[服务]
公共类DependentService:服务,iSeries服务
{
公开作废开始()
{
var intent=新的intent(Android.App.Application.Context,
类型(从属服务);
if(Android.OS.Build.VERSION.SdkInt>=Android.OS.BuildVersionCodes.O)
{
Android.App.Application.Context.StartForegroundService(intent);
}
其他的
{
Android.App.Application.Context.StartService(intent);
}
}
公共覆盖iBind OnBind(意图)
{
返回null;
}
public const int SERVICE_RUNNING_NOTIFICATION_ID=10000;
公共覆盖StartCommandResult OnStartCommand(意图,
StartCommandFlags标志,int startId)
{
//从共享代码或您的PCL中
CreateNotificationChannel();
字符串messageBody=“服务启动”;
var notification=new notification.Builder(这是“10111”)
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(messageBody)
.SetSmallIcon(Resource.Drawable.main)
.正在进行(正确)
.Build();
StartForeground(服务运行通知ID,通知);
//你工作吗
返回StartCommandResult.Sticky;
}
void CreateNotificationChannel()
{
if(Build.VERSION.SdkInt
这里有一条关于你的需求的类似线索。

用于IOS后台任务。您可以参考

由于或稍后,正常服务将在后台终止

在Android 8.0或更高版本中,我建议您实现一个目标以实现这一目标(接收比“常规”服务更高的优先级,前台服务必须提供一个通知,只要服务运行,Android就会显示)

您可以使用dependence service以xamarin表单打开前台服务

IService.cs为android创建一个启动服务的界面

public interface IService
{
  void Start();
}
然后实现
DependentService
以启动前台服务

DependentService.cs

[assembly: Xamarin.Forms.Dependency(typeof(DependentService))]
namespace TabGuesture.Droid
{
  [Service]
public class DependentService : Service, IService
{
public void Start()
{
    var intent = new Intent(Android.App.Application.Context, 
    typeof(DependentService));


    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
    {
        Android.App.Application.Context.StartForegroundService(intent);
    }
    else
    {
        Android.App.Application.Context.StartService(intent);
    }
}

public override IBinder OnBind(Intent intent)
{
    return null;
}
public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;
public override StartCommandResult OnStartCommand(Intent intent, 
StartCommandFlags flags, int startId)
{
    // From shared code or in your PCL

    CreateNotificationChannel();
    string messageBody = "service starting";

    var notification = new Notification.Builder(this, "10111")
    .SetContentTitle(Resources.GetString(Resource.String.app_name))
    .SetContentText(messageBody)
    .SetSmallIcon(Resource.Drawable.main)
    .SetOngoing(true)
    .Build();
    StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
    //do you work
    return StartCommandResult.Sticky;
}


void CreateNotificationChannel()
{
    if (Build.VERSION.SdkInt < BuildVersionCodes.O)
    {
        // Notification channels are new in API 26 (and not a part of the
        // support library). There is no need to create a notification
        // channel on older versions of Android.
        return;
    }

    var channelName = Resources.GetString(Resource.String.channel_name);
    var channelDescription = GetString(Resource.String.channel_description);
    var channel = new NotificationChannel("10111", channelName, NotificationImportance.Default)
    {
        Description = channelDescription
    };

    var notificationManager = (NotificationManager)GetSystemService(NotificationService);
    notificationManager.CreateNotificationChannel(channel);
 }
}
}
[assembly:Xamarin.Forms.Dependency(typeof(DependentService))]
名称空间TabGuesture.Droid
{
[服务]
公共类DependentService:服务,iSeries服务
{
公开作废开始()
{
var intent=新的intent(Android.App.Application.Context,
类型(从属服务);
if(Android.OS.Build.VERSION.SdkInt>=Android.OS.BuildVersionCodes.O)
{
Android.App.Application.Context.StartForegroundService(intent);
}
其他的
{
Android.App.Application.Context.StartService(intent);
}
}
公共覆盖iBind OnBind(意图)
{
返回null;
}
public const int SERVICE_RUNNING_NOTIFICATION_ID=10000;
公共覆盖StartCommandResult OnStartCommand(意图,
StartCommandFlags标志,int startId)
{
//从共享代码或您的PCL中
CreateNotificationChannel();
字符串messageBody=“服务启动”;
var notification=new notification.Builder(这是“10111”)
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(messageBody)
.SetSmallIcon(Resource.Drawable.main)
.正在进行(正确)
.Build();
StartForeground(服务运行通知ID,通知);
//你工作吗
返回StartCommandResult.Sticky;
}
void CreateNotificationChannel()
{
if(Build.VERSION.SdkInt