简体   繁体   English

phonegap、iphone 和大坏蛋 idleTimerDisabled

[英]phonegap, iphone and the big bad idleTimerDisabled

reading a lot about how to prevent iphone goingto sleep while running my app I am very unhappy at the moment because nothing worked..阅读了很多关于如何在运行我的应用程序时防止 iphone 进入睡眠状态的信息,我目前非常不高兴,因为没有任何效果..

here I read about the idea to set up a timer for every 30 seconds to set the idleTimerDisabled to NO and then YES, but my objC isn't that good yet. 在这里,我读到了每隔 30 秒设置一个计时器以将 idleTimerDisabled 设置为 NO 然后是 YES 的想法,但是我的 objC 还不是那么好。 could anybody tell me how (and where)?谁能告诉我如何(以及在哪里)?

thnx!谢谢!

edit: here is the code I tried:编辑:这是我尝试过的代码:

- (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: after that I tried to start a loop with: 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];


}

edit3: you really can't change accidentially downvotes?编辑3:你真的不能意外地改变投票吗? would be a nice chnage request to the stackoverflow system!将是对 stackoverflow 系统的一个很好的更改请求!

Regarding your question about using a timer :关于您关于使用计时器的问题:

Here is how you make a timer go off (just once) in 30 seconds from now:以下是如何在 30 秒后关闭(仅一次)计时器 go:

-(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]

If you want two different timers , after say 30 and then 60 seconds, just make two in the same way.如果你想要两个不同的计时器,在 30 秒和 60 秒之后,只需以相同的方式制作两个。 Let me know if you need more help with timers!如果您在计时器方面需要更多帮助,请告诉我!


It couldn't be easier.这再简单不过了。 Just add this line:只需添加这一行:

application.idleTimerDisabled = YES;

Inside your "application didFinishLaunchingWithOptions" routine.在您的“应用程序 didFinishLaunchingWithOptions”例程中。

You will find that routine inside your app delegate .m source code file.您将在您的应用程序委托.m 源代码文件中找到该例程。

Be sure to add it BEFORE the "return YES;"请务必在“return YES;”之前添加它statement - a common mistake, So: exactly like this:声明 - 一个常见的错误,所以:完全像这样:

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

    application.idleTimerDisabled = YES;
    return YES;
    }

Just setting [UIApplication sharedApplication].idleTimerDisabled = YES;只需设置 [UIApplication sharedApplication].idleTimerDisabled = YES; in

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

works well for me.很适合我。 However, there is a caveat.但是,有一个警告。 I have noticed that every time I invoke camera utility from my phonegap app to take a snapshot, idleTimerDisable gets set to NO behind the scene.我注意到,每次我从 phonegap 应用程序调用相机实用程序来拍摄快照时,idleTimerDisable 都会在幕后设置为 NO。 So right after i upload my image, I had call the following line of code again:因此,在我上传图片后,我再次调用了以下代码行:

[UIApplication sharedApplication].idleTimerDisabled = YES; [UIApplication sharedApplication].idleTimerDisabled = YES;

Also, I would not be surprised if there are more places throughout the app idle timer gets reenabled.此外,如果整个应用程序空闲计时器有更多地方重新启用,我也不会感到惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM