gpt4 book ai didi

ios - 设备从休眠状态唤醒后后台位置更新未恢复

转载 作者:行者123 更新时间:2023-12-01 17:07:58 25 4
gpt4 key购买 nike

我有一个问题,应用程序正在注册后台位置更新,直到设备休眠,但在设备从休眠状态恢复后,位置更新不会恢复。

该应用程序非常简单,因为它可以测量位置更新对电池生命周期的影响。

我使用单 View 应用程序模板创建了应用程序,并且:

  • 将下面发布的代码添加到应用程序委托(delegate)(仅此而已,没有其他
    代码)
  • 新增位置更新后台模式
  • 添加 coreLocation.framework

  • 当我启动应用程序然后将其移至后台时,didUpdateLocations: 继续被连续调用约 15-20 分钟,并且 getDataUsage() 每隔几秒执行一次。

    然后在大约 15-20 分钟后,设备进入休眠状态(我正在监控 Organizer 控制台上的事件)。
    但是,在唤醒设备后,应用程序永远不会收到任何 didUpdateLocations: 调用。
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.timeThatDataMonitoringStarted = [NSDate date];
    self.initialDataValuesSet = NO;

    CLLocationDegrees degreesFilter = 0.1;
    [self.locationManager setHeadingFilter: degreesFilter];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = 10;
    [self.locationManager startUpdatingLocation];

    [self getDataUsage];

    return YES;
    }

    - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
    NSLog(@"******************** LOCATION didUpdateLocations ***************");
    static NSDate* previousDate;
    static int count = 0;
    if (previousDate)
    {
    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:previousDate];
    NSLog(@" LOCATION: count: %d time since last update: %f", ++count, timeInterval);
    }
    previousDate = [NSDate date];
    }

    - (void) getDataUsage
    {
    NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate: self.timeThatDataMonitoringStarted];
    NSLog(@"********* GETTING DATA USAGE. Elapsed time: %f **************",elapsedTime);
    NSArray *data = [self getDataCounters];

    NSNumber *wifiSentSinceBoot = (NSNumber*)data[0];
    NSNumber *wifiReceivedSinceBoot = (NSNumber*)data[1];
    NSNumber *wwanSentSinceBoot = (NSNumber*)data[2];
    NSNumber *wwanReceivedSinceBoot = (NSNumber*)data[3];

    int wifiSentSinceBootAsInt = [wifiSentSinceBoot intValue];
    int wifiReceivedSinceBootAsInt = [wifiReceivedSinceBoot intValue];
    int wWanSentSinceBootAsInt = [wwanSentSinceBoot intValue];
    int wWanReceivedSinceBootAsInt = [wwanReceivedSinceBoot intValue];

    static int initialWifiSent;
    static int initialWifiReceived;
    static int initialWWanSent;
    static int initialWWanReceived;
    if (!self.initialDataValuesSet)
    {
    self.initialDataValuesSet = YES;
    initialWifiSent = wifiSentSinceBootAsInt;
    initialWifiReceived = wifiReceivedSinceBootAsInt;
    initialWWanSent = wWanSentSinceBootAsInt;
    initialWWanReceived = wWanReceivedSinceBootAsInt;
    }

    int wifiSentSinceLastRetrieval = wifiSentSinceBootAsInt - initialWifiSent;
    int wifiReceivedSinceLastRetrieval = wifiReceivedSinceBootAsInt - initialWifiReceived;
    int wWanSentSinceLastRetrieval = wWanSentSinceBootAsInt - initialWWanSent;
    int wWanReceivedSinceLastRetrieval = wWanReceivedSinceBootAsInt - initialWWanReceived;

    uint dataUsed = wifiSentSinceLastRetrieval + wifiReceivedSinceLastRetrieval + wWanSentSinceLastRetrieval + wWanReceivedSinceLastRetrieval;
    NSLog(@"Total data: %d", dataUsed);
    [self performSelector:@selector(getDataUsage) withObject:nil afterDelay:3];
    }


    - (NSArray *) getDataCounters
    {
    BOOL success;
    struct ifaddrs *addrs;
    const struct ifaddrs *cursor;
    const struct if_data *networkStatisc;

    int WiFiSent = 0;
    int WiFiReceived = 0;
    int WWANSent = 0;
    int WWANReceived = 0;

    NSString *name=[[NSString alloc]init];

    success = getifaddrs(&addrs) == 0;
    if (success)
    {
    cursor = addrs;
    while (cursor != NULL)
    {
    name=[NSString stringWithFormat:@"%s",cursor->ifa_name];
    // NSLog(@"ifa_name %s == %@\n", cursor->ifa_name,name);
    // names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN

    if (cursor->ifa_addr->sa_family == AF_LINK)
    {
    if ([name hasPrefix:@"en"])
    {
    networkStatisc = (const struct if_data *) cursor->ifa_data;
    WiFiSent+=networkStatisc->ifi_obytes;
    WiFiReceived+=networkStatisc->ifi_ibytes;
    // NSLog(@"WiFiSent %d ==%d",WiFiSent,networkStatisc->ifi_obytes);
    // NSLog(@"WiFiReceived %d ==%d",WiFiReceived,networkStatisc->ifi_ibytes);
    }

    if ([name hasPrefix:@"pdp_ip"])
    {
    networkStatisc = (const struct if_data *) cursor->ifa_data;
    WWANSent+=networkStatisc->ifi_obytes;
    WWANReceived+=networkStatisc->ifi_ibytes;
    // NSLog(@"WWANSent %d ==%d",WWANSent,networkStatisc->ifi_obytes);
    // NSLog(@"WWANReceived %d ==%d",WWANReceived,networkStatisc->ifi_ibytes);
    }
    }

    cursor = cursor->ifa_next;
    }

    freeifaddrs(addrs);
    }

    return [NSArray arrayWithObjects:[NSNumber numberWithInt:WiFiSent], [NSNumber numberWithInt:WiFiReceived],[NSNumber numberWithInt:WWANSent],[NSNumber numberWithInt:WWANReceived], nil];
    }

    注意我使用的术语“休眠”不是指应用程序状态,而是指设备状态。如果您在 Organizer 的控制台中观察到设备事件,那么一段时间后,整个设备就会开始关闭。我称之为休眠是因为我不知道实际的 iOS 术语是什么,而且休眠也不是指自动锁定和屏幕休眠。之后这是一个单独的事件,我假设电话和 GPS 芯片等正在断电。
    无论如何,在达到此“休眠”阶段之后,如果您在设备上恢复事件,则当我没有得到 didUpdateLocations 时。

    最佳答案

    您需要从 App 委托(delegate)方法中移动您的 getDataUsage 方法调用:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    到 CLLocationManager 委托(delegate):
    - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

    由于每次发生位置更新(调用上面的委托(delegate)),那么你的 getDataUsage 就会被调用。

    尝试使用 performSelector 使用伪“For 循环”是不好的设计,并且在应用程序处于后台模式时会导致不可预知的结果。因此,您需要删除该行:
    [self performSelector:@selector(getDataUsage) withObject:nil afterDelay:3];

    如果您打算让应用程序在后台运行,我还将确保该应用程序已注册为后台模式。

    我还会在此处的答案中查看更多详细信息:
    iOS7 Core Location not updating

    关于ios - 设备从休眠状态唤醒后后台位置更新未恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22969519/

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