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)

Xamarin 后台持续定位与提示的更多相关文章

  1. iOS 后台持续定位详解(支持ISO9.0以上)

    iOS 后台持续定位详解(支持ISO9.0以上) #import <CoreLocation/CoreLocation.h>并实现CLLocationManagerDelegate 代理, ...

  2. App切换到后台后如何保持持续定位?

    为了保护用户隐私,大多数应用只会在前台运行时获取用户位置,当应用在后台运行时,定位功能会被禁止.这就导致APP在后台或者锁屏时无法正常记录GPS轨迹,这对打车.共享出行.跑步等需要实时记录用户轨迹的应 ...

  3. Xamarin.Android之定位

    一.前言 打开我们手中的应用,可以发现越来越多的应用使用了定位,从而使我们的生活更加方便,所以本章我们将学习如何在Xamarin中进行定位的开发. 二.准备工作 因为我们的虚拟机是运行在电脑本地的,自 ...

  4. 解决iOS地图持续定位耗电问题

    地图位置刷新的代理didUpdateLocations会持续调用,手机非常耗电 但是在实际开发中,有一些APP确实需要用到持续定位的功能,比如:运动类, 导航类, 天气类等等 如何进行持续定位呢?保证 ...

  5. Blazor组件自做七 : 使用JS隔离制作定位/持续定位组件

    1. 运行截图 演示地址 2. 在文件夹wwwroot/lib,添加geolocation子文件夹,添加geolocation.js文件 本组件主要是调用浏览器两个API实现基于浏览器的定位功能,现代 ...

  6. C#先执行一段sql等后台操作后再提示是否后续操作confrim

    应用场景:例如选择一个单据号打击打印后先去数据库检索是否有打打印过,如果有则提示,已打印,是否再打 如果没有则不提示,直接进行打印. 实现原理:多做一个隐藏按钮去实现打印功能,页面上的打印按钮则进行数 ...

  7. 程序自启动及后台持续运行的研究(voip和GPS方式)

    1,voip方式    1)首先要修改应用的plist配置,添加下面的设置:        Application does not run in background: NO        Requ ...

  8. iOS开发雕虫小技之傻瓜式定位神器-超简单方式解决iOS后台定时定位

    1.概述 由于公司一款产品的需求,最近一直在研究iOS设备的后台定位.主要的难点就是,当系统进入后台之后,程序会被挂起,届时定时器.以及代码都不会Run~ 所以一旦用户将我的App先换到了后台,我的定 ...

  9. Asp.Net后台弹出确认提示窗Confirm

    前端js代码: function MyConfirm(message, guid) { if (confirm(message) == true) { document.getElementById( ...

随机推荐

  1. pojg487-3279电话号码转换(字符映射)

    http://poj.grids.cn/practice/2974 注意输入中连字符可以任意添加和删除. 描述企业喜欢用容易被记住的电话号码.让电话号码容易被记住的一个办法是将它写成一个容易记住的单词 ...

  2. oschina娱乐游戏

    休闲游戏 123网络游戏 88单机游戏 201游戏模拟器/工具/引擎

  3. c++ - Create empty json array with jsoncpp - Stack Overflow

    python中multiprocessing.pool函数介绍_正在拉磨_新浪博客     multiprocessing.pool c++ - Create empty json array wit ...

  4. 《编程之美》学习笔记——指挥CPU占用率

    问题: 写一个程序.让用户来决定Windows任务管理器(Task Manager)的CPU占用率(单核). 有下面几种情况: 1.CPU占用率固定在50%,为一条直线 2.CPU的占用率为一条直线, ...

  5. 物理Data Guard主备切换步骤

    物理Data Guard角色转换步骤   Step  1   验证主库是否能执行角色转换到备库(原主库执行) SQL> SELECT SWITCHOVER_STATUS FROM V$DATAB ...

  6. 异常configure: *** apu library not found.

    安装modsecurity时,出现"configure: *** apu library not found.".      解决方法: yum install apr-util- ...

  7. C# - 委托_求定积分通用方法

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  8. 在Windows下编译OpenSSL(VS2005和VC6)

    需要说明的是请一定安装openssl-0.9.8a .  openssl-1.0.0我没有编译成功. 如何在Windows下编译OpenSSL (Vs2005使用Vc8的cl编译器)1.安装Activ ...

  9. 求助(VC++) 隐藏Console窗体无效

    [逝去的100~~ 2014/10/07 20: 20] 程序想要实现控制台窗体的隐藏,可是窗体每次执行总会弹出来.为什么呢? 代码例如以下: // Mini.cpp : 定义控制台应用程序的入口点. ...

  10. CentOS6 yum源支持更多rpm包的升级(使用第三方软件库EPEL、RPMForge与RPMFusion)

    转载于http://blog.csdn.net/erazy0/article/details/6878153 在CentOS下运行yum install flash-plugin或yum instal ...