在我的應用程序中,我想在應用程序進入後臺時記錄加速度計數據(由系統授予的過期時間將足夠)。加速器在beginBackgroundTaskWithExpirationHandler中初始化時加速沒有被調用?
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIDevice* device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)])
backgroundSupported = device.multitaskingSupported;
if (backgroundSupported) {
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
// Do the work associated with the task.
if ([application backgroundTimeRemaining] > 1.0) {
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = kUpdateInterval;
}
});
在我提到的像代表:
- (void) accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration {
if (acceleration.x > kAccelerationThreshold ||
acceleration.y > kAccelerationThreshold ||
acceleration.z > kAccelerationThreshold) {
NSLog(@"Sensed acceleration");
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
然而,didAccelerate當應用程序進入背景不被觸發,而是被applicationWillEnterForeground後叫我如下已編碼的這一點。應用程序從後臺重新啓動後,didAccelerate似乎被多次觸發(可能是加速度計值在後臺更改的次數)。當應用程序進入前臺時,請求似乎堆積並執行。
有沒有什麼辦法可以記錄加速度計值時,應用程序在後臺狀態( 內beginBackgroundTaskWithExpirationHandler)
請幫助。
一個更正:我剛剛驗證了加速度計數據,它似乎只捕獲applicationDidEnterForeground後的值。 – CyberKnight 2010-12-12 12:24:49