2013-10-01 75 views
4

當我們的應用程序在後臺時是否可以檢查電池電量?iOS應用程序檢查背景中的電池電量

目前我工作的一個iPhone應用程序中,當電池達到一定級別的用戶將被提醒。

我已經搜索在谷歌/持股比例溢出。但找不到有用的東西。我可以在應用程序處於前景狀態時確定電池電量。但我們都知道蘋果不允許應用程序在後臺運行。

所以我的問題是怎樣才能得到當前電池電量事件,即使我的應用程序是在後臺。

這裏是類似的應用。它是一個付費應用程序但通過看截圖,你會得到想要我想說的想法。

https://itunes.apple.com/it/app/battery-alert!/id416283462?mt=8

+0

的可能重複[我可以查看iPhone的電池狀態在後臺狀態?(http://stackoverflow.com/questions/7705373/can-i-check-battery-status-of-iphone-in-background-state) – Pang

回答

0
UIDevice *device = [UIDevice currentDevice]; 
device.batteryMonitoringEnabled = YES; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device]; 

// Your notification handler 
- (void)batteryChanged:(NSNotification *)notification 
{ 
    UIDevice *device = [UIDevice currentDevice]; 
    NSLog(@"state: %i | charge: %f", device.batteryState, device.batteryLevel); 
} 

退房這段代碼...

+0

我會試試這段代碼。我懷疑它是否能在後臺工作。 –

+1

在後臺不起作用。 [A懸浮應用程序不執行任何代碼,因此不能處理的通知,包括電池更改通知(https://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/DOC/UID/TP40007072-CH4-SW45)。 – Pang

相關問題