2015-09-21 59 views
6

我希望在手機鎖定和解鎖以及手機變爲空白(長時間不活動時)之後讓我的應用程序監視,這一切都是在我的應用程序未聚焦時,但在後臺運行。如何在應用程序在後臺運行時收聽鎖定/解鎖手機事件?

我可以接收鎖定/解鎖/空白事件與易用性,同時應用的重點是:

-(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真實的設備測試它。

+0

我不介意在Swift中的解決方案。 –

+0

僅在應用程序中啓用後臺模式沒有幫助,應用程序實際上應該在後臺運行。當你鎖定/解鎖手機時,你能否確認你的應用程序實際上是在後臺運行? – user3334059

+0

@SumantHanumante,蘋果有沒有限制在後臺運行,聽取鎖定,解鎖事件? –

回答

-1

即使你的應用程序被配置爲在後臺運行,它不會實際運行,如果它沒有工作要做。爲了得到它在後臺與位置更新運行,請按照these instructions by Ricky

  1. 我重新啓動位置管理器功能didUpdateLocations每1分鐘。
  2. 我允許位置管理器在關閉設備之前從設備獲取位置10秒鐘(以節省電池)。

部分代碼如下圖:

-(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的項目,聽取了鎖定和解鎖運行iOS 9設備上的事件,使用最新的XCode。這裏是日誌:

2015-12-18 13:31:44.777 Location[16185:3796448] startLocationTracking 
2015-12-18 13:31:44.780 Location[16185:3796448] authorizationStatus authorized 
2015-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 didUpdateLocations 
2015-12-18 13:31:44.837 Location[16185:3796448] started master task 1 
2015-12-18 13:31:45.197 Location[16185:3796448] locationManager didUpdateLocations 
2015-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!
+0

有人可以解釋爲什麼這個答案是downvoted?我試着添加一條評論,它說我沒有足夠的業力,只能添加一個答案 – user3272750

相關問題