Xamarin 后台持续定位与提示
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 后台持续定位与提示的更多相关文章
- iOS 后台持续定位详解(支持ISO9.0以上)
iOS 后台持续定位详解(支持ISO9.0以上) #import <CoreLocation/CoreLocation.h>并实现CLLocationManagerDelegate 代理, ...
- App切换到后台后如何保持持续定位?
为了保护用户隐私,大多数应用只会在前台运行时获取用户位置,当应用在后台运行时,定位功能会被禁止.这就导致APP在后台或者锁屏时无法正常记录GPS轨迹,这对打车.共享出行.跑步等需要实时记录用户轨迹的应 ...
- Xamarin.Android之定位
一.前言 打开我们手中的应用,可以发现越来越多的应用使用了定位,从而使我们的生活更加方便,所以本章我们将学习如何在Xamarin中进行定位的开发. 二.准备工作 因为我们的虚拟机是运行在电脑本地的,自 ...
- 解决iOS地图持续定位耗电问题
地图位置刷新的代理didUpdateLocations会持续调用,手机非常耗电 但是在实际开发中,有一些APP确实需要用到持续定位的功能,比如:运动类, 导航类, 天气类等等 如何进行持续定位呢?保证 ...
- Blazor组件自做七 : 使用JS隔离制作定位/持续定位组件
1. 运行截图 演示地址 2. 在文件夹wwwroot/lib,添加geolocation子文件夹,添加geolocation.js文件 本组件主要是调用浏览器两个API实现基于浏览器的定位功能,现代 ...
- C#先执行一段sql等后台操作后再提示是否后续操作confrim
应用场景:例如选择一个单据号打击打印后先去数据库检索是否有打打印过,如果有则提示,已打印,是否再打 如果没有则不提示,直接进行打印. 实现原理:多做一个隐藏按钮去实现打印功能,页面上的打印按钮则进行数 ...
- 程序自启动及后台持续运行的研究(voip和GPS方式)
1,voip方式 1)首先要修改应用的plist配置,添加下面的设置: Application does not run in background: NO Requ ...
- iOS开发雕虫小技之傻瓜式定位神器-超简单方式解决iOS后台定时定位
1.概述 由于公司一款产品的需求,最近一直在研究iOS设备的后台定位.主要的难点就是,当系统进入后台之后,程序会被挂起,届时定时器.以及代码都不会Run~ 所以一旦用户将我的App先换到了后台,我的定 ...
- Asp.Net后台弹出确认提示窗Confirm
前端js代码: function MyConfirm(message, guid) { if (confirm(message) == true) { document.getElementById( ...
随机推荐
- iOS 本地化应用程序(NSLocalizedString)
App本地化的需要不用讲大家也都明白,本文将介绍一种简单的方法来实现字符串的本地化. 在不考虑本地化的情况下,我们如果在代码中给一个Button定义title,一般会这样写: btn.titleLab ...
- C++0x简讯
关于C++0x核心进展的一组简讯 刘未鹏 /文 C++的罗浮宫(http://blog.csdn.net/pongba) Concepts无疑是C++0x的杀手级特性之中的一个(也许称它“杀手级”另一 ...
- Duanxx的C++得知:计算位数
一旦计算出一个数值数字,基本上它是不断分裂使用10.重新计,看看有多少个数字. 今天发现能够考虑先将数字转换为字符串,然后通过string.length获得数值的位数,这样做方便的多. string ...
- 再造 “手机QQ” 侧滑菜单(一)——实现侧滑效果
本系列文章中,我们将尝试再造手机QQ的侧滑菜单,力争最大限度接近手Q的实际效果,并使用 Auto Layout 仿造左侧菜单,实现和主视图的联动. 代码示例:https://github.com/jo ...
- ACM第三次比赛UVA11877 The Coco-Cola Store
Once upon a time, there is a special coco-cola store. If you return three empty bottles to the sho ...
- 分析一个socket通信: server/client
分析一个socket通信: server/client1 server 1. 创建一个server_socket文件,并绑定端口,然后监听端口 (socket, bind, listen) 2. 查询 ...
- iOS 用GDataXMLNode创建和解析XML
原文地址:http://blog.csdn.net/gf771115/article/details/7718403 NSError *error; // NSString *path = [[ ...
- CentOS6.5+php5.3听云安装过程
听云安装过程请参考听云安装指导页面:http://doc.tingyun.com/help/html/doc/server_novice.html 根据自己的服务器系统和语言包下载对应的 安装包 安装 ...
- 输入输出函数 I/O函数之perror()
perror()函数的函数原型 void perror(char const *message); 它会将message信息输出出来,后面再加上错误原因字符串. 下面是来自百度百科的实例: #incl ...
- 使用jQuery的attr方法来修改onclick值
这篇文章主要介绍了通过jQuery的attr修改onclick值的解决方法 ,需要的朋友可以参考下 var js = "alert('B:' + this.id); return false ...