From 7167334c0e89dd84827d59e726123d14776e3a09 Mon Sep 17 00:00:00 2001 From: 陈嘉乐 <cjl@hdlchina.com.cn> Date: 星期二, 16 六月 2020 11:14:13 +0800 Subject: [PATCH] 2020-06-16-1 --- Shared.IOS/BaseApplicationDelegate.cs | 109 +++++++++++++++++++++++++++++++++--------------------- 1 files changed, 67 insertions(+), 42 deletions(-) diff --git a/Shared.IOS/BaseApplicationDelegate.cs b/Shared.IOS/BaseApplicationDelegate.cs index 077856f..95d4a1c 100644 --- a/Shared.IOS/BaseApplicationDelegate.cs +++ b/Shared.IOS/BaseApplicationDelegate.cs @@ -21,11 +21,37 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { registerForRemoteNotification(application); - var musicInfo = new MusicInfo { }; - Volume.Init(); + //var musicInfo = new MusicInfo { }; + //Volume.Init(); FinishedLaunchingAction?.Invoke(application, launchOptions); - + //GPS + if (Shared.Application.IsGpsEnable) + { + if (MyCLLocationManager.Instance.IsLocationServicesEnabled) + { + switch (Shared.Application.CurrentGpsUseMode) + { + case Application.GpsUseMode.Always: + MyCLLocationManager.Instance.RequestAlwaysAuthorization(); + break; + case Application.GpsUseMode.WhenInUse: + MyCLLocationManager.Instance.RequestWhenInUseAuthorization(); + break; + } + if (launchOptions != null && launchOptions.ObjectForKey(UIApplication.LaunchOptionsLocationKey) != null) + { + MyCLLocationManager.Instance.StartMonitoringSignificantLocationChanges(); + } + else + { + MyCLLocationManager.Instance.StartUpdatingLocation(); + } + } + else { + ///TODO 鍙互鎻愮ず鐢ㄦ埛鎵撳紑瀹氫綅鏉冮檺 + } + } return true; } @@ -49,9 +75,10 @@ application.RegisterForRemoteNotificationTypes(UIRemoteNotificationType.NewsstandContentAvailability | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound); } } - + class MyCLLocationManager : CLLocationManager { + #region 瀹氫綅 public static MyCLLocationManager Instance { get @@ -61,12 +88,11 @@ locationManager = new MyCLLocationManager(); //璁剧疆鏄惁鍏佽绯荤粺鑷姩鏆傚仠瀹氫綅 locationManager.PausesLocationUpdatesAutomatically = false; - //locationManager.AllowsBackgroundLocationUpdates = true; + locationManager.AllowsBackgroundLocationUpdates = false; //璁剧疆瀹氫綅绮惧害 locationManager.DesiredAccuracy = CLLocation.AccuracyKilometer; locationManager.init(); } - return locationManager; } } @@ -92,22 +118,23 @@ locationManager.LocationsUpdated += (sender, e) => { var location = e.Locations[e.Locations.Length - 1]; - //adjustDistanceFilter(location); - System.Console.WriteLine($"浣嶇疆淇℃伅鍙樺寲 缁忓害{location.Coordinate.Longitude} 绾害{location.Coordinate.Latitude}"); - uploadToServer(location); + adjustDistanceFilter(location); + Shared.Application.LocationAction?.Invoke(location.Coordinate.Longitude, location.Coordinate.Latitude); + //System.Console.WriteLine($"浣嶇疆淇℃伅鍙樺寲 缁忓害{location.Coordinate.Longitude} 绾害{location.Coordinate.Latitude}"); + //uploadToServer(location); }; } } - /** - * 瑙勫垯: 濡傛灉閫熷害灏忎簬minSpeed m/s 鍒欐妸瑙﹀彂鑼冨洿璁惧畾涓�100m - * 鍚﹀垯灏嗚Е鍙戣寖鍥磋瀹氫负minSpeed*minInteval - * 姝ゆ椂鑻ラ�熷害鍙樺寲瓒呰繃10% 鍒欐洿鏂板綋鍓嶇殑瑙﹀彂鑼冨洿(杩欓噷闄愬埗鏄洜涓轰笉鑳戒笉鍋滅殑璁剧疆distanceFilter, - * 鍚﹀垯uploadLocation浼氫笉鍋滆瑙﹀彂) -*/ + + //* 瑙勫垯: 濡傛灉閫熷害灏忎簬minSpeed m/s 鍒欐妸瑙﹀彂鑼冨洿璁惧畾涓�100m + //* 鍚﹀垯灏嗚Е鍙戣寖鍥磋瀹氫负minSpeed*minInteval + //* 姝ゆ椂鑻ラ�熷害鍙樺寲瓒呰繃10% 鍒欐洿鏂板綋鍓嶇殑瑙﹀彂鑼冨洿(杩欓噷闄愬埗鏄洜涓轰笉鑳戒笉鍋滅殑璁剧疆distanceFilter, + //* 鍚﹀垯uploadLocation浼氫笉鍋滆瑙﹀彂) + void adjustDistanceFilter(CLLocation location) { - // NSLog(@"adjust:%f",location.speed); + //NSLog(@"adjust:%f",location.speed); if (location.Speed < minSpeed) { if (Math.Abs(DistanceFilter - minInteval) > 0.1f) @@ -145,32 +172,30 @@ } } - void uploadToServer(CLLocation location) { -#warning 濡傛灉鏈夎緝闀挎椂闂寸殑鎿嶄綔 姣斿HTTP涓婁紶 璇蜂娇鐢╞eginBackgroundTaskWithExpirationHandler - if (UIApplication.SharedApplication.ApplicationState == UIApplicationState.Active) - { - //HTTP upload - Shared.Application.LocationAction?.Invoke(location.Coordinate.Longitude, location.Coordinate.Latitude); - - endBackgroundUpdateTask(); - } - else//鍚庡彴瀹氫綅 - { - //鍋囧涓婁竴娆$殑涓婁紶鎿嶄綔灏氭湭缁撴潫 鍒欑洿鎺eturn - if (taskIdentifier != UIApplication.BackgroundTaskInvalid) - { - return; - } - - beingBackgroundUpdateTask(); - - //HTTP upload - Shared.Application.LocationAction?.Invoke(location.Coordinate.Longitude, location.Coordinate.Latitude); - - //涓婁紶瀹屾垚璁板緱璋冪敤 - endBackgroundUpdateTask(); - } - } +// void uploadToServer(CLLocation location) +// { +//#warning 濡傛灉鏈夎緝闀挎椂闂寸殑鎿嶄綔 姣斿HTTP涓婁紶 璇蜂娇鐢╞eginBackgroundTaskWithExpirationHandler +// if (UIApplication.SharedApplication.ApplicationState == UIApplicationState.Active) +// { +// //HTTP upload +// Shared.Application.LocationAction?.Invoke(location.Coordinate.Longitude, location.Coordinate.Latitude); +// endBackgroundUpdateTask(); +// } +// else//鍚庡彴瀹氫綅 +// { +// //鍋囧涓婁竴娆$殑涓婁紶鎿嶄綔灏氭湭缁撴潫 鍒欑洿鎺eturn +// if (taskIdentifier != UIApplication.BackgroundTaskInvalid) +// { +// return; +// } +// beingBackgroundUpdateTask(); +// //HTTP upload +// Shared.Application.LocationAction?.Invoke(location.Coordinate.Longitude, location.Coordinate.Latitude); +// //涓婁紶瀹屾垚璁板緱璋冪敤 +// endBackgroundUpdateTask(); +// } +// } + #endregion } public override void OnResignActivation(UIApplication application) => OnResignActivationAction?.Invoke(application);// Invoked when the application is about to move from active to inactive state.// This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) // or when the user quits the application and it begins the transition to the background state.// Games should use this method to pause the game. -- Gitblit v1.8.0