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. mysql 创建临时节点

    use ZooKeeper; use AnyEvent; use AE; use Data::Dumper; use IO::Socket; my $zk = ZooKeeper->new(ho ...

  2. android JB2连拍降速原理介绍

    1.HAL层 (1)alps\mediatek\platform\mt6589\hardware\camera\core\camshot\MultiShot\MultiShot.cpp sleep实现 ...

  3. SimpleAdapter

    1.视图 1)主视图 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  4. java--join方法

    package MyTest; class TestDemo implements Runnable { public void run() { int i = 0; for (int j = 0; ...

  5. 演练2-4:CodeFirst实例之“电影网站制作”

    原文出处:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model EntityFr ...

  6. Spring MVC视图层:thymeleaf vs. JSP

    本文对比了同一Spring MVC工程中相同页面(一个订阅表单)分别采用Thymeleaf和JSP(包括JSP.JSTL.Spring tag lib)两种方式的实现. 本文的所有代码来自一个可运行的 ...

  7. CairoSVG - Convert SVG to PNG or PDF - Contents

    CairoSVG - Convert SVG to PNG or PDF - Contents User Documentation Author Guillaume Ayoub Date 2011- ...

  8. Oracle10g数据泵EXPDP和IMPDP备份与恢复数据

    Oracle10g数据泵EXPDP和IMPDP备份与恢复数据 一.数据库备份前准备工作 新建备份DIRECTORY目录,并授权给用户 步骤: 1.登录sqlplus 账户名:ptemp 密码:0000 ...

  9. perl encode_json 会产生 UTF-8 (binary) string decode_json 需要一个 UTF-8 (binary) string

    encode_json $json_text = encode_json $perl_scalar Converts the given Perl data structure to a UTF-8 ...

  10. UVa 657 掷骰子

    意甲冠军:有一个大图.每个像素是格孩子只可能是 . * X 三种.代表背景.玻色子.色子点. 两格子是邻近或在通信,当且仅当两个格儿子*要么X.且具有共同的边,这是上下左右四个方向,斜过,即四连块. ...