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( ...
随机推荐
- KVM客户机使用主机USB设备
有些时候KVM客户机还是要使用USB设备,比如USB密钥等 KVM命令行参数 -usb 打开usb驱动程序,启动客户机usb支持 -usbdevice devname 为客户机增加usb设备,devn ...
- 通过sharedpreference两个程序共享数据
一.整体工程图 二.SharePreferenceWriteActivity.java package org.ourunix.android.sharepreferencewrite; i ...
- Linux: 信息查看
Linux log日志查看 http://www.2cto.com/os/201307/227230.html
- BZOJ 1057: [ZJOI2007]棋盘制作( dp + 悬线法 )
对于第一问, 简单的dp. f(i, j)表示以(i, j)为左上角的最大正方形, f(i, j) = min( f(i + 1, j), f(i, j + 1), f(i + 1, j + 1)) ...
- [转载] iOS开发分辨率那点事
1 iOS设备的分辨率 iOS设备,目前最主要的有3种(Apple TV等不在此讨论),按分辨率分为两类 iPhone/iPod Touch 普屏分辨率 320像素 x 480像素 Retina ...
- Android--pendingIntent & Intent
PendingIntent pendingIntent字面意义:等待的,未决定的Intent.要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, ...
- SpringMVC日期类型转换问题三大处理方法归纳
方法一:实体类中加日期格式化注解 @DateTimeFormat(pattern = "yyyy-MM-dd") private Date receiveAppTime; 方法二: ...
- LifecyclePhaseNotFoundException(转)
This error is generated if you try to invoke a build command that Maven does not understand. In gene ...
- c++怎样让返回对象的函数不调用拷贝构造函数
我们知道拷贝构造函数有两种“默默”的方式被调用 1. 想函数传入 值参数 2. 函数返回 值类型 今天我们讨论函数返回值类型的情况. 得到结论是 1. 当对象有拷贝构造函数(系统为我们生成.或者我们自 ...
- hdu 1166 敌兵布阵(线段树,树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1166 直接线段树模板 AC代码: #include<iostream> #include<cs ...