gpt4 book ai didi

ios - 如何在应用程序在后台运行时监听锁定/解锁电话事件?

转载 作者:可可西里 更新时间:2023-11-01 04:04:00 24 4
gpt4 key购买 nike

我想让我的应用程序监控手机何时锁定和解锁,以及何时变为空白(长时间不活动后),所有这些都是在我的应用程序未获得焦点,而是在后台运行时进行的。

当应用程序处于焦点状态时,我可以轻松接收锁定/解锁/空白事件:

-(void) startListeningForPhoneLockEvent
{
NSLog(@"Start listening to lock/unlock and screen-goes-black events.");

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
(void*)self,
lockStateChanged,
CFSTR("com.apple.springboard.lockstate"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
(void*)self,
hasBlankedScreen,
CFSTR("com.apple.springboard.hasBlankedScreen"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}

和回调函数:

static void lockStateChanged( CFNotificationCenterRef center, void*observer, CFStringRef name, const void *object, CFDictionaryRef userInfo )
{
NSLog(@"Lock event received!");
}

static void hasBlankedScreen( CFNotificationCenterRef center, void*observer, CFStringRef name, const void *object, CFDictionaryRef userInfo )
{
NSLog(@"Blanked screen event received!");
}

我启用了后台模式:

  • 后台获取。

但是,一旦应用程序进入后台,它就不会收到锁定/解锁/黑屏事件。

我已经尝试过其他后台模式,例如声音播放、位置更新等,但应用程序在后台时仍然没有收到锁定/解锁/黑屏事件。

我不确定这是否真的可行,或者我是否做错了什么。

我正在更新到 iOS9 的真实设备上使用最新的 XCode 和 iOS9 SDK 对其进行测试。

最佳答案

即使您的应用配置为在后台运行,如果它没有工作要做,它也不会真正运行。要让它在后台运行位置更新,请遵循 these instructions by Ricky :

  1. I restart the location manager every 1 minute in function didUpdateLocations.
  2. I allow the location manager to get the locations from the device for 10 seconds before shut it down (to save battery).

Partial Code Below:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

for(int i=0;i<locations.count;i++){
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];

if (locationAge > 30.0)
continue;

//Select only valid location and also location with good accuracy
if(newLocation!=nil&&theAccuracy>0
&&theAccuracy<2000
&&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){
self.myLastLocation = theLocation;
self.myLastLocationAccuracy= theAccuracy;
NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
[dict setObject:[NSNumber numberWithFloat:theLocation.latitude] forKey:@"latitude"];
[dict setObject:[NSNumber numberWithFloat:theLocation.longitude] forKey:@"longitude"];
[dict setObject:[NSNumber numberWithFloat:theAccuracy] forKey:@"theAccuracy"];
//Add the vallid location with good accuracy into an array
//Every 1 minute, I will select the best location based on accuracy and send to server
[self.shareModel.myLocationArray addObject:dict];
}
}

//If the timer still valid, return it (Will not run the code below)
if (self.shareModel.timer)
return;

self.shareModel.bgTask = [BackgroundTaskManager sharedBackgroundTaskManager];
[self.shareModel.bgTask beginNewBackgroundTask];

//Restart the locationMaanger after 1 minute
self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
selector:@selector(restartLocationUpdates)
userInfo:nil
repeats:NO];

//Will only stop the locationManager after 10 seconds, so that we can get some accurate locations
//The location manager will only operate for 10 seconds to save battery
NSTimer * delay10Seconds;
delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self
selector:@selector(stopLocationDelayBy10Seconds)
userInfo:nil
repeats:NO];
}

我能够在此处引用的 github 项目中使用您的代码,以使用最新的 XCode 监听运行 ios 9 的设备上的锁定和解锁事件。这是日志:

2015-12-18 13:31:44.777 Location[16185:3796448] startLocationTracking2015-12-18 13:31:44.780 Location[16185:3796448] authorizationStatus authorized2015-12-18 13:31:44.788 Location[16185:3796448] Start listening to lock/unlock and screen-goes-black events.2015-12-18 13:31:44.834 Location[16185:3796448] locationManager didUpdateLocations2015-12-18 13:31:44.837 Location[16185:3796448] started master task 12015-12-18 13:31:45.197 Location[16185:3796448] locationManager didUpdateLocations2015-12-18 13:31:48.079 Location[16185:3796448] Blanked screen event received!2015-12-18 13:31:48.215 Location[16185:3796448] Lock event received!

关于ios - 如何在应用程序在后台运行时监听锁定/解锁电话事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32691754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com