我有一个应用程序,当用户多次按下电源按钮时,我想触发该应用程序。该应用程序当前作为后台进程工作,并侦听CFNotificationCenterRef,它在显示状态更改时触发。这太敏感了。当设备接收到任何类型的通知时,显示器会自动更改,这也会触发事件。有没有办法只检测电源按钮?
大部分代码都来自Detect screen on/off from iOS service
我将观察者注册为后台进程
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.iokit.hid.displayStatus"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
这是代码处理事件。
static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
NSLog(@"event received!");
NSLog(@"Darwin notification center = %@",center);
NSLog(@"Darwin notification observer = %@",observer);
NSLog(@"Darwin notification NAME = %@",name);
NSLog(@"Darwin notification object = %@",object);
NSLog(@"Darwin notification userInfo = %@",userInfo);
[masterViewController powerButtonTrigger];
// you might try inspecting the `userInfo` dictionary, to see
// if it contains any useful info
if (userInfo != nil) {
CFShow(userInfo);
}
}
以下是这些事件的日志:
2015-06-14 09:17:56.715 MayDay[1268:273855] event received!
2015-06-14 09:17:56.716 MayDay[1268:273855] Darwin notification center = <CFNotificationCenter 0x1740f5e00 [0x195da9f50]>
2015-06-14 09:17:56.716 MayDay[1268:273855] Darwin notification observer = (null)
2015-06-14 09:17:56.716 MayDay[1268:273855] Darwin notification NAME = com.apple.iokit.hid.displayStatus
2015-06-14 09:17:56.717 MayDay[1268:273855] Darwin notification object = (null)
2015-06-14 09:17:56.717 MayDay[1268:273855] Darwin notification userInfo = (null)
2015-06-14 09:17:56.717 MayDay[1268:273855] powerButtonTrigger
2015-06-14 09:17:56.717 MayDay[1268:273855] press number 1 first press was 3 seconds ago
2015-06-14 09:17:57.452 MayDay[1268:273855] event received!
2015-06-14 09:17:57.453 MayDay[1268:273855] Darwin notification center = <CFNotificationCenter 0x1740f5e00 [0x195da9f50]>
2015-06-14 09:17:57.454 MayDay[1268:273855] Darwin notification observer = (null)
2015-06-14 09:17:57.566 MayDay[1268:273855] Darwin notification NAME = com.apple.iokit.hid.displayStatus
2015-06-14 09:17:57.566 MayDay[1268:273855] Darwin notification object = (null)
2015-06-14 09:17:57.582 MayDay[1268:273855] Darwin notification userInfo = (null)
2015-06-14 09:17:57.583 MayDay[1268:273855] powerButtonTrigger
2015-06-14 09:17:57.583 MayDay[1268:273855] press number 2 first press was 4 seconds ago
2015-06-14 09:17:58.896 MayDay[1268:273855] event received!
2015-06-14 09:17:58.898 MayDay[1268:273855] Darwin notification center = <CFNotificationCenter 0x1740f5e00 [0x195da9f50]>
2015-06-14 09:17:58.898 MayDay[1268:273855] Darwin notification observer = (null)
2015-06-14 09:17:58.898 MayDay[1268:273855] Darwin notification NAME = com.apple.iokit.hid.displayStatus
2015-06-14 09:17:58.898 MayDay[1268:273855] Darwin notification object = (null)
2015-06-14 09:17:58.899 MayDay[1268:273855] Darwin notification userInfo = (null)
2015-06-14 09:17:58.899 MayDay[1268:273855] powerButtonTrigger
2015-06-14 09:17:58.900 MayDay[1268:273855] press number 3 first press was 5 seconds ago
2015-06-14 09:17:59.653 MayDay[1268:273855] event received!
2015-06-14 09:17:59.654 MayDay[1268:273855] Darwin notification center = <CFNotificationCenter 0x1740f5e00 [0x195da9f50]>
2015-06-14 09:17:59.655 MayDay[1268:273855] Darwin notification observer = (null)
2015-06-14 09:17:59.770 MayDay[1268:273855] Darwin notification NAME = com.apple.iokit.hid.displayStatus
2015-06-14 09:17:59.770 MayDay[1268:273855] Darwin notification object = (null)
2015-06-14 09:17:59.783 MayDay[1268:273855] Darwin notification userInfo = (null)
2015-06-14 09:17:59.784 MayDay[1268:273855] powerButtonTrigger
2015-06-14 09:17:59.784 MayDay[1268:273855] press number 4 first press was 6 seconds ago