有沒有辦法知道我的設備(iPhone)何時插入電源,例如帶有USB端口的電腦或汽車音響系統?我在我的應用中使用本地化服務,並且我想在設備插入時自動更改爲kCLLocationAccuracyBestForNavigation
。謝謝...如何知道iOS設備何時插入?
5
A
回答
3
您可以註冊以在配件連接或斷開連接時收到通知。
例子:
[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(accessoryDidConnect:)
name:EAAccessoryDidConnectNotification
object:nil];
[notificationCenter addObserver:self
selector:@selector(accessoryDidDisconnect:)
name:EAAccessoryDidDisconnectNotification
object:nil];
一旦你收到此通知,您可以使用一個for循環遍歷每個附件,如:
NSArray *accessories = [[EAAccessoryManager sharedAccessoryManager] connectedAccessories];
EAAccessory *accessory = nil;
for (EAAccessory *obj in accessories)
{
// See if you're interested in this particular accessory
}
在某一點(的dealloc也許)你將要註銷爲這些通知。你可以做到這一點,如:
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self
name:EAAccessoryDidDisconnectNotification
object:nil];
[notificationCenter removeObserver:self
name:EAAccessoryDidConnectNotification
object:nil];
[[EAAccessoryManager sharedAccessoryManager] unregisterForLocalNotifications];
7
您可以啓用電池監控直通的UIDevice class並檢查電池狀態,看它是否正在充電:
typedef enum {
UIDeviceBatteryStateUnknown,
UIDeviceBatteryStateUnplugged,
UIDeviceBatteryStateCharging,
UIDeviceBatteryStateFull,
} UIDeviceBatteryState;
你要檢查或收費在啓用最佳GPS準確性之前已滿。通過讓你自己的操作方法batteryStateChanged
通話
UIDeviceBatteryState batteryState = [[UIDevice currentDevice] batteryState];
要訂閱通知,關於電池狀態的變化,例如:
- (void) setup {
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
NSNotificationCenter * center= [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(batteryStateChanged)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
}
2
要檢查電池狀態記得取消訂閱當你的對象是dealloced:
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] setBatteryMonitoringEnabled:NO];
}
相關問題
- 1. 如何知道Android設備何時插入USB端口?
- 2. 如何知道設備何時充電?
- 3. iOS:如何知道設備語言何時更改?
- 4. 如何知道設備?
- 5. 如何知道插入了哪種設備?
- 6. 我們如何知道通知到達iOS設備?
- 7. 知道設備何時完全打開
- 8. 有沒有辦法知道iOS設備何時鎖定/解鎖?
- 9. 如何識別何時將USB設備插入WinCE設備
- 10. 如何在iOS設備插入時檢查「Debug View Hierarchy」?
- 11. 如何知道iframe何時準備好?
- 12. 多設備插入通知
- 13. 如何知道設備的密度?
- 14. 如何正確知道設備方向
- 15. 如何知道USB設備「HID」和「PID」
- 16. 我如何知道iOS設備現在連接到互聯網?
- 17. 如何知道iOS設備處於某個附近?
- 18. NSNetService - 我如何知道它是Mac還是iOS設備?
- 19. 知道手機何時進入Android設備上的建築物
- 20. 如何知道BLE設備何時訂閱Android上的特徵?
- 21. 如何知道設備是android還是移動設備?
- 22. 想知道如何使用select插入
- 23. 如何知道連接到系統的輸入設備
- 24. 如何知道何時在MonoMac中設置了控制插座?
- 25. 如何知道ios編程中的ios設備的擺動方向?
- 26. Android設備啓動時如何知道「啓動」
- 27. 客戶端(iOS設備)如何知道服務器的任何更改?
- 28. 如何知道何時在Angular 2中設置了[輸入]?
- 29. 如何檢測音頻設備何時插入和拔出?
- 30. 如何將耳機插入3.5接口時知道?
**非常感謝**我會tes t此代碼... – human4 2012-02-16 21:21:54
@ human4總是樂於提供幫助。如果'UIDevice'的'batteryState'上的KVO正常工作,那麼這就是我想要的。 – Sam 2012-02-16 22:22:05
這不再起作用。 – 2015-01-23 08:53:15