gpt4 book ai didi

ios - 在后台运行 NSTimer 或 UILocalNotification

转载 作者:可可西里 更新时间:2023-11-01 06:16:55 26 4
gpt4 key购买 nike

如何在 iOS 上执行倒计时?

首先,我尝试了 NSTimer。然后我发现当应用程序进入后台时 NSTimer 无法工作。

其次,我用过

counterTask = [[UIApplication sharedApplication]
beg
inBackgroundTaskWithExpirationHandler:^{
}];

NSTimer 在后台工作。但后来我发现 iOS 只允许应用程序在后台运行不超过 600 秒(10 分钟)。我不知道如何让 NSTimer 在后台运行超过 10 分钟。

第三,我厌倦了使用NSLocalNotification,但它运行良好。但我想做的是通过调用一个方法来暂停播放音乐。我发现 UILocalNotification 会在时间到了时显示警报。如果您没有单击警报按钮,方法 - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification 将不会被调用。如果不单击警报按钮,我不知道如何调用方法。

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(@"Recieved Notification %@",notification);
UIAlertView *alert=[[[UIAlertView alloc] initWithTitle:@"Local Notification" message:@"message"
delegate:nil cancelButtonTitle:nil
otherButtonTitles:nil, nil] autorelease];
[alert show];
}

所以,问题是1.如何让NSTimer在后台运行超过10分钟,或者2. 如何在 UILocalNotification 时调用我自己的方法而不单击警报按钮。

非常感谢!!!

最佳答案

counterTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
}];

在到期处理程序中放置一些东西,这将创建另一个计时器,该计时器具有另一个到期处理程序,该计时器创建另一个计时器,该计时器具有另一个处理程序...

关于ios - 在后台运行 NSTimer 或 UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8216573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com