locationManager = [[CLLocationManager alloc] init];locationManager.delegate =self; //or whatever class you have for managing location
if(![CLLocationManager locationServicesEnabled] || ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied))//判断定位服务是否打开
{
[InterfaceFuncation ShowAlertWithMessage:@"错误"AlertMessage:@"定位服务未打开\n保持在线需要后台定位服务\n请到 设置-隐私 中打开定位服务"ButtonTitle:@"我错了"];
return;
}
@property(assign, nonatomic) UIBackgroundTaskIdentifier bgTask;
@property(strong, nonatomic) dispatch_block_t expirationHandler;
@property(assign, nonatomic)BOOL jobExpired;
@property(assign, nonatomic)BOOL background;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
UIApplication* app = [UIApplication sharedApplication];
__weak NSUAAAIOSAppDelegate* selfRef =self;
self.expirationHandler = ^{ //创建后台自唤醒,当180s时间结束的时候系统会调用这里面的方法
[app endBackgroundTask:selfRef.bgTask];
selfRef.bgTask = UIBackgroundTaskInvalid;
selfRef.bgTask = [app beginBackgroundTaskWithExpirationHandler:selfRef.expirationHandler];
NSLog(@"Expired");
selfRef.jobExpired =YES;
while(selfRef.jobExpired)
{
// spin while we wait for the task to actually end.
NSLog(@"等待180s循环进程的结束");
[NSThreadsleepForTimeInterval:1];
}
// Restart the background task so we can run forever.
[selfRef startBackgroundTask];
};
// Assume that we're in background at first since we get no notification from device that we're in background when
// app launches immediately into background (i.e. when powering on the device or when the app is killed and restarted)
[selfmonitorBatteryStateInBackground];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate =self;
//[locationManager startUpdatingLocation];
returnYES;
}
- (void)monitorBatteryStateInBackground
{
self.background =YES;
[selfstartBackgroundTask];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
NSLog(@"App is active");
[UIApplication sharedApplication].applicationIconBadgeNumber=0;//取消应用程序通知脚标
[locationManager stopUpdatingLocation];
self.background =NO;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
//if([self bgTask])
if(isLogined)//当登陆状态才启动后台操作
{
self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:self.expirationHandler];
NSLog(@"Entered background");
[selfmonitorBatteryStateInBackground];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError*)error//当定位服务不可用出错时,系统会自动调用该函数
{
NSLog(@"定位服务出错");
if([error code]==kCLErrorDenied)//通过error的code来判断错误类型
{
//Access denied by user
NSLog(@"定位服务未打开");
[InterfaceFuncation ShowAlertWithMessage:@"错误"AlertMessage:@"未开启定位服务\n客户端保持后台功能需要调用系统的位置服务\n请到设置中打开位置服务"ButtonTitle:@"好"];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations//当用户位置改变时,系统会自动调用,这里必须写一点儿代码,否则后台时间刷新不管用
{
NSLog(@"位置改变,必须做点儿事情才能刷新后台时间");
CLLocation *loc = [locations lastObject];
//NSTimeInterval backgroundTimeRemaining = [[UIApplication sharedApplication] backgroundTimeRemaining];
//NSLog(@"Background Time Remaining = %.02f Seconds",backgroundTimeRemaining);
// Lat/Lon
floatlatitudeMe = loc.coordinate.latitude;
floatlongitudeMe = loc.coordinate.longitude;
}
- (void)startBackgroundTask
{
NSLog(@"Restarting task");
if(isLogined)//当登陆状态才进入后台循环
{
// Start the long-running task.
NSLog(@"登录状态后台进程开启");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// When the job expires it still keeps running since we never exited it. Thus have the expiration handler
// set a flag that the job expired and use that to exit the while loop and end the task.
NSIntegercount=0;
BOOLNoticeNoBackground=false;//只通知一次标志位
BOOLFlushBackgroundTime=false;//只通知一次标志位
locationManager.distanceFilter = kCLDistanceFilterNone;//任何运动均接受,任何运动将会触发定位更新
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;//定位精度
while(self.background && !self.jobExpired)
{
NSLog(@"进入后台进程循环");
[NSThreadsleepForTimeInterval:1];
count++;
if(count>60)//每60s进行一次开启定位,刷新后台时间
{
count=0;
[locationManager startUpdatingLocation];
NSLog(@"开始位置服务");
[NSThreadsleepForTimeInterval:1];
[locationManager stopUpdatingLocation];
NSLog(@"停止位置服务");
FlushBackgroundTime=false;
}
if(!isLogined)//未登录或者掉线状态下关闭后台
{
NSLog(@"保持在线进程失效,退出后台进程");
[InterfaceFuncation ShowLocalNotification:@"保持在线失效,登录已被注销,请重新登录"];
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
return;//退出循环
}
NSTimeIntervalbackgroundTimeRemaining = [[UIApplication sharedApplication] backgroundTimeRemaining];
NSLog(@"Background Time Remaining = %.02f Seconds",backgroundTimeRemaining);
if(backgroundTimeRemaining<30&&NoticeNoBackground==false)
{
[InterfaceFuncation ShowLocalNotification:@"向系统申请长时间保持后台失败,请结束客户端重新登录"];
NoticeNoBackground=true;
}
//测试后台时间刷新
if(backgroundTimeRemaining>200&&FlushBackgroundTime==false)
{
[[NSNotificationCenterdefaultCenter] postNotificationName:@"MessageUpdate"object:@"刷新后台时间成功\n"];
FlushBackgroundTime=true;
//[InterfaceFuncation ShowLocalNotification:@"刷新后台时间成功"];
}
}
self.jobExpired =NO;
});
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有