I am playing an audio file in the app, so it doesn't turn off when the phone is locked and the screen is off. However when I 开发者_开发百科want detect shake it does not work.
It works fine when the app is open and when the screen is locked (not off). The app is definitely running because the logs are working fine.
Any idea?
I use the following code:
-(BOOL)canBecomeFirstResponder {
//make it respond to shake events
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
//make it respond to shake events
[self becomeFirstResponder];
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake) {
NSLog(@"Shake detected");
}
}
Monitoring the accelerometer causes extra power consumption by the phone, so it is disabled while the phone is locked. I don't believe there is a way around this.
This would help you.
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
This wont let the screen turn off.
Hope this helps.
EDIT:
But Even I feel the same as Akshay does. That it is not possible to capture UIEvent when screen is off.
I don't think you can capture UIEvents
when the screen is off.
精彩评论