gpt4 book ai didi

iOS 多任务处理不工作

转载 作者:行者123 更新时间:2023-11-29 13:32:58 25 4
gpt4 key购买 nike

- (IBAction)buttonPressed:(id)sender
{
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
[self beingBackgroundUpdateTask];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];
[self currentBatteryState];


}


- (void) beingBackgroundUpdateTask
{
self->backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}


- (void) endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self->backgroundUpdateTask];
self->backgroundUpdateTask = UIBackgroundTaskInvalid;
}

由于某种原因,通知没有被遵守。难道我做错了什么?我想在不插电的情况下观察长达 10 分钟

最佳答案

您不应该在 buttonPressed: 方法中调用 endBackgroundUpdateTask,因为那样会取消您的后台任务。尝试删除此代码:

if ([[UIDevice currentDevice] isMultitaskingSupported]) {
[self endBackgroundUpdateTask];
}

此外,您应该将 UIDeviceBatteryLevelDidChangeNotification 常量传递给“name”参数,而不是字符串。它应该看起来像这样(注意缺少双引号):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:device];

(也有可能 UIDevice 根本不在后台发送这些通知。)

关于iOS 多任务处理不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11377186/

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