3

阅读了很多关于如何在运行我的应用程序时防止 iphone 进入睡眠状态的信息,我目前非常不高兴,因为没有任何效果..

在这里,我读到了每隔 30 秒设置一个计时器以将 idleTimerDisabled 设置为 NO 然后是 YES 的想法,但是我的 objC 还不是那么好。谁能告诉我如何(以及在​​哪里)?

谢谢!

编辑:这是我尝试过的代码:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{   
    [ super applicationDidFinishLaunching:application ];
    //application.idleTimerDisabled = NO;
    //application.idleTimerDisabled = YES;
    //[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
    [UIApplication sharedApplication].idleTimerDisabled = NO;
    [UIApplication sharedApplication].idleTimerDisabled = YES;


}

edit2:之后我尝试使用以下方法开始循环:

-(void)_timerLoop
{
    // add this function up the top.  it's what will happen when the
    // timer goes off:
    NSLog(@"Your timer went off!!!");
}


/**
 * This is main kick off after the app inits, the views and Settings are setup here.
 */
- (void)applicationDidFinishLaunching:(UIApplication *)application
{   
    [ super applicationDidFinishLaunching:application ];
    //application.idleTimerDisabled = NO;
    //application.idleTimerDisabled = YES;
    //[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
    //[UIApplication sharedApplication].idleTimerDisabled = NO;
    //[UIApplication sharedApplication].idleTimerDisabled = YES;
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(_timerLoop) userInfo:nil repeats:YES];


}

编辑3:你真的不能意外地改变投票吗?将是对 stackoverflow 系统的一个很好的更改请求!

4

2 回答 2

3

关于您关于使用计时器的问题:

以下是如何让计时器在 30 秒后关闭(仅一次):

-(void)_jump
{
// add this function up the top.  it's what will happen when the
// timer goes off:
NSLog(@"Your timer went off!!!");
}
...
// here's how to create the timer, which will go off in 30 seconds
[NSTimer scheduledTimerWithTimeInterval:30.0
   target:self selector:@selector(_jump) userInfo:nil repeats:NO]

如果你想要两个不同的计时器,在 30 秒和 60 秒之后,只需以相同的方式制作两个。如果您在计时器方面需要更多帮助,请告诉我!


这再简单不过了。只需添加这一行:

application.idleTimerDisabled = YES;

在您的“应用程序 didFinishLaunchingWithOptions”例程中。

您将在您的应用程序委托.m 源代码文件中找到该例程。

请务必在“return YES;”之前添加它 声明 - 一个常见的错误!所以,就像这样:

-(BOOL)application:(UIApplication *)application
            didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    // blah blah ...

    application.idleTimerDisabled = YES;
    return YES;
    }
于 2011-04-28T20:02:33.390 回答
1

只需设置 [UIApplication sharedApplication].idleTimerDisabled = YES; 在

  • (BOOL) 应用程序:(UIApplication*) 应用程序 didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

很适合我。但是,有一个警告。我注意到,每次我从 phonegap 应用程序调用相机实用程序来拍摄快照时,idleTimerDisable 都会在幕后设置为 NO。因此,在我上传图片后,我再次调用了以下代码行:

[UIApplication sharedApplication].idleTimerDisabled = YES;

此外,如果整个应用程序空闲计时器有更多地方重新启用,我也不会感到惊讶。

于 2012-07-27T14:16:13.270 回答