发布:2021/11/9 16:18:56作者:管理员 来源:本站 浏览次数:2104
我每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 2014 - 2025 柏港建站平台 ejk5.com. 渝ICP备16000791号-4