本文转载至 http://gaohaijun.blog.163.com/blog/static/176698271201161524857373/
问题:那位能说一下怎么能检测到iphone的屏幕现在是否在被点亮的状态,还是被关闭的黑屏状态, 要用那个api ,给点提示 谢谢了。
复制代码
- - (void)didTurnOnDisplay;
- - (void)didTurnOffDisplay;
|
没有测试,但是相信可以。
刚刚测试,假如是子类化UIApplication是无法接受到这个方法的。用Objc Runtime的method_exchangeImplementations(method, method)就可以。
以下代码:
UIApplication方面:
复制代码
- @interface UIApplication (_)
- - (void)handleDidTurnOnDisplay;
- - (void)handleDidTurnOffDisplay;
- @end
- @implementation UIApplication (_)
- - (void)handleDidTurnOnDisplay
- {
- NSLog(@"Did turn on display.");
- //[self handleDidTurnOnDisplay];
-
- }
- - (void)handleDidTurnOffDisplay
- {
- NSLog(@"Did turn off display.");
- //[self handleDidTurnOffDisplay];
- }
- @end
|
UIApplicationDelegate:
复制代码
- - (void)applicationDidFinishLaunching:(UIApplication *)application {
-
- Method m1 = class_getInstanceMethod([UIApplication class], @selector(didTurnOnDisplay));
- Method m2 = class_getInstanceMethod([UIApplication class], @selector(didTurnOffDisplay));
-
- Method m3 = class_getInstanceMethod([UIApplication class], @selector(handleDidTurnOnDisplay));
- Method m4 = class_getInstanceMethod([UIApplication class], @selector(handleDidTurnOffDisplay));
-
- method_exchangeImplementations(m1, m3);
- method_exchangeImplementations(m2, m4);
-
- [window makeKeyAndVisible];
- }
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- NSLog(@"Application will resign active.");
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- NSLog(@"Application did become active.");
- }
|
以上代码在打开后关闭屏幕再打开的条件下输出:
引用
2009-08-17 19:05:37.770 *[380:207] Application did become active.
2009-08-17 19:06:36.370 *[380:207] Did turn off display.
2009-08-17 19:06:36.452 *[380:207] Application will resign active.
2009-08-17 19:06:43.310 *[380:207] Did turn on display.
2009-08-17 19:06:43.415 *[380:207] Application did become active.
可见这个是方法在delegate的willResignActive和didBecomeActive之前调用。
其实屏幕在变黑时, 会通知程序
- (void)applicationWillResignActive:(UIApplication *)application;
而解锁后变亮会触发
- (void)applicationDidBecomeActive:(UIApplication *)application;