IOS后台持续运行对于c#程序员不懂得ios后台机制的是存在一定困扰的。特别是ios9过后对后台和安全进行了更严格的限制

好了废话不多说

一 设置info.plist权限信息

参考:

后台模式:http://www.codeceo.com/article/ios-background-guide.html

后台持续定位:http://www.jianshu.com/p/4e30b0af8f51

播放声音:http://www.jianshu.com/p/e54751348abe

需要开启后台运行模式:勾选位置更新,音频播放

二 关键代码

2.1 设置

 coo.DistanceFilter = -;//设置两点之间位置精度,即手机不动也会提示
coo.DesiredAccuracy = CLLocation.AccuracyBest;
coo.AllowsBackgroundLocationUpdates = true;//开启后台持续定位
coo.PausesLocationUpdatesAutomatically = false;//禁用后台暂停位置更新
coo.Delegate = this;
coo.RequestAlwaysAuthorization(); coo.StartUpdatingLocation();
coo.StartUpdatingHeading();

2.2 前后台切换

        public override void DidEnterBackground(UIApplication uiApplication)
{
       //进入后台
DependencyService.Get<ILocationManager>().EnterBackground();
} public override void WillEnterForeground(UIApplication uiApplication)
{
       //进入前台
DependencyService.Get<ILocationManager>().EnterForground();
}

三 播放音频(系统提示音)

var sound = new SystemSound();

await sound.PlaySystemSoundAsync();

四 实现结果

当位置发生更新时播放系统提示音,且伴有震动,这里暂时对ios消息推送不甚了解,下次将在位置更新播放提示音同时加入本地推送

这里是使用xamarin.ios 原生api实现,之前试过百度地图持续最新sdk弄了半天进入后台就不行了,也许是我知识不够,但是使用原生api也行那么也就不用那么麻烦了

五 完整代码

[assembly: Dependency(typeof(MyLocationManager))]

namespace Xamarin.Simples.iOS
{
public class MyLocationManager : NSObject, ICLLocationManagerDelegate, ILocationManager
{
private CLLocationManager coo;
private bool init = false;
public MyLocationManager()
{
coo = new CLLocationManager(); } #region ICLLocationManagerDelegate #region IDisposable implementation void IDisposable.Dispose()
{
throw new NotImplementedException();
} #endregion #region INativeObject implementation IntPtr ObjCRuntime.INativeObject.Handle
{
get
{
throw new NotImplementedException();
}
} #endregion [Foundation.Export("locationManager:didUpdateLocations:")]
public async void LocationsUpdated(CoreLocation.CLLocationManager manager, CoreLocation.CLLocation[] locations)
{
Console.WriteLine("定位成功"); var sound = new SystemSound(); await sound.PlaySystemSoundAsync(); } [Export("locationManager:didUpdateHeading:")]
public void UpdatedHeading(CoreLocation.CLLocationManager manager, CoreLocation.CLHeading newHeading)
{ } [Foundation.Export("locationManager:didUpdateToLocation:fromLocation:")]
public void UpdatedLocation(CoreLocation.CLLocationManager manager, CoreLocation.CLLocation newLocation, CoreLocation.CLLocation oldLocation)
{ } #endregion public void Init()
{
if (!init)
{
coo.DistanceFilter = -;
coo.DesiredAccuracy = CLLocation.AccuracyBest;
coo.AllowsBackgroundLocationUpdates = true;
coo.PausesLocationUpdatesAutomatically = false;
coo.Delegate = this;
coo.RequestAlwaysAuthorization(); coo.StartUpdatingLocation();
coo.StartUpdatingHeading(); init = true;
} } public void EnterBackground()
{
if (init)
coo.StartMonitoringSignificantLocationChanges();
} public void EnterForground()
{
if (init)
coo.StopMonitoringSignificantLocationChanges();
} }
}

六  案例下载

https://yunpan.cn/cqegpGW96nYx7 (提取码:4508)

最新文章

  1. obj转json
  2. 原生js事件的添加和删除
  3. 我需要在Web上完成一个图片上传的功能
  4. kendo ui的treeView节点点击事件修改和grid的配置的一点总结
  5. Exception Handling in ASP.NET Web API webapi异常处理
  6. [转载] 关于PreparedStatement.addBatch()方法
  7. 【JSP jstl c标签】使用c:foreach 报错(警告)”test does not support runtime expressions“
  8. ConcurrentHashMap原理分析(1.7与1.8)
  9. python基础6之迭代器&amp;生成器、json&amp;pickle数据序列化
  10. python集合操作
  11. bootstrap学习: 基本组件以及布局;
  12. ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例
  13. Spring源码导入IDEA
  14. python线程和进程编程对比
  15. docker部署路飞学城
  16. MTP 写字机器
  17. git学习笔记(四)—— 分支管理
  18. C#索引器理解
  19. P3877 [TJOI2010]打扫房间
  20. wamp中mysql安装时能启动,重启后无法启动的解决办法

热门文章

  1. Animation 的setFillAfter
  2. 管理tips
  3. GridView控件-01-[简单的数据显示]
  4. 距离变换DT
  5. lua 安装配置
  6. centos安装python gcc sqlite
  7. KestrelServer
  8. SignalR系列教程:在MVC5中使用SignalR
  9. Android NFC传输联系人VCF
  10. MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件