我每15分钟监视一次用户的位置,我只是希望应用程序继续发送该位置,即使用户在任务栏中关闭了该应用程序也是如此.
我尝试了此示例,但它在Xamarin.Android中 https://docs.microsoft.com/zh-cn/xamarin/android/app-fundamentals/services/foreground-services 我必须创建一个dependencyservice,但是我不知道如何.
解决方案我必须创建一个依赖服务,但我不知道如何.
首先,在Xamarin.forms项目中创建一个Interface:
public interface IStartService
{
void StartForegroundServiceCompat();
}
然后创建一个新文件,在xxx.Android项目中将其命名为itstartServiceAndroid,以实现所需的服务:
[assembly: Dependency(typeof(startServiceAndroid))]
namespace DependencyServiceDemos.Droid
{
public class startServiceAndroid : IStartService
{
public void StartForegroundServiceCompat()
{
var intent = new Intent(MainActivity.Instance, typeof(myLocationService));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
MainActivity.Instance.StartForegroundService(intent);
}
else
{
MainActivity.Instance.StartService(intent);
}
}
}
[Service]
public class myLocationService : Service
{
public override IBinder OnBind(Intent intent)
{
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
// Code not directly related to publishing the notification has been omitted for clarity.
// Normally, this method would hold the code to be run when the service is started.
//Write want you want to do here
}
}
}
一旦要在Xamarin.forms项目中调用StartForegroundServiceCompat方法,则可以使用:
public MainPage()
{
InitializeComponent();
//call method to start service, you can put this line everywhere you want to get start
DependencyService.Get<IStartService>().StartForegroundServiceCompat();
}
以下是有关依赖关系的文档服务
对于iOS,如果用户在任务栏中关闭应用程序,则您将不再能够运行任何服务.如果应用程序正在运行,则可以阅读有关 ios-backgrounding-walkthroughs/location-walkthrough
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有