对于位置管理器,iOS7和iOS6的电池消耗是否有任何区别?

时间:2013-10-10 17:23:51

标签: objective-c ios6 ios7 cllocationmanager

@implementation MTCLocationManager
@synthesize manager;
@synthesize lastLocation;
@synthesize timer;
@synthesize wsUserName;
@synthesize xmlParser;
@synthesize webData;
@synthesize soapResults;
@synthesize wsUserPassword;
@synthesize glbData;
@synthesize vAktarDelegate;

NSDate *firstDate;
NSUserDefaults *defaults;
bool isUploadingDataNow;
NSString *strUserName;
NSString *strPassword;
NSString *strUrl;
CLLocation *lastLocation;
int saveUserApplicationStatusCalled;


bool sendOnly1Record = false;
-(id)init{

    self = [super init];

    if (self) {

        CLLocationManager *locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;

        [self setLocationManager:locationManager withDistanceFilter:100.0f];
        lastLocation = [[CLLocation alloc]init];
        firstDate =[NSDate date];
        glbData = [[NSMutableString alloc] init];
        defaults = [NSUserDefaults standardUserDefaults];
        strUserName= [defaults stringForKey:@"username"];
        strPassword= [defaults stringForKey:@"password"];
        strUrl = [defaults stringForKey:@"url"];
        isUploadingDataNow = false;
        saveUserApplicationStatusCalled = 0;

    }

    return self;
}

-(void) setSendOnly1Record:(BOOL) s{
    sendOnly1Record = s;

}
-(void)enteredBackground:(id)sender{
    [self.manager startMonitoringSignificantLocationChanges];
}

-(void)startUpdate{
   [self.manager startUpdatingLocation];
}
-(void) stopLocationListener
{
    [self.manager stopMonitoringSignificantLocationChanges];
}

-(void)post1Record:(CLLocation *)newLocation STATUS:(int) iStat{
    NSLog(@"\n\nPOST1RECORD");
    NSString *latitude = [[NSString alloc] init];
    NSString *longitude = [[NSString alloc] init];
    latitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].latitude];
    longitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].longitude];
    NSDate *currdate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [[NSString alloc] init];
    dateString = [dateFormatter stringFromDate:currdate];


    NSString* batString = @"";
    @try {
        [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
        float batteryLevel = [[UIDevice currentDevice] batteryLevel];
        NSLog(@"battery level %f",batteryLevel);
        batteryLevel *= 100;
        int batLevel = (int) batteryLevel;
        batString= [NSString stringWithFormat:@"%i", abs(batLevel)];
    }
    @catch (NSException *exception) {

    }
    @finally {

    }
    NSMutableString *strTemp = [[NSMutableString alloc] init];
    NSString* strStat = [NSString stringWithFormat:@"%i", iStat];
    [strTemp appendString:dateString];
    [strTemp appendString:@"|"];
    [strTemp appendString:latitude];
    [strTemp appendString:@"|"];
    [strTemp appendString:longitude];
    [strTemp appendString:@"|"];
    [strTemp appendString:batString];
    [strTemp appendString:@"|"];
    [strTemp appendString:strStat];
    [strTemp appendString:@"|"];

    [self postMULTILocation:strTemp];

}

- (void)locationManager:(CLLocationManager *)theLocationManager didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation{
    lastLocation = newLocation;
    if(sendOnly1Record) {
        sendOnly1Record = false;
        saveUserApplicationStatusCalled =1;
        [self post1Record:newLocation STATUS:0];
    }
    else if (!isUploadingDataNow) {


        NSString *latitude = [[NSString alloc] init];
        NSString *longitude = [[NSString alloc] init];
        latitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].latitude];
        longitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].longitude];
        NSDate *currdate = [NSDate date];
        int intervall = (int) [currdate timeIntervalSinceDate: firstDate] / 60;
        NSLog(@"time interval %d",intervall);
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *dateString = [[NSString alloc] init];
        dateString = [dateFormatter stringFromDate:currdate];

        if(intervall > 10) {
            //update location
            isUploadingDataNow = true;
            [self postMULTILocation:glbData];

            firstDate = [NSDate date];
        }else{


            NSString* batString = @"";
            @try {
                [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
                float batteryLevel = [[UIDevice currentDevice] batteryLevel];
                //NSLog(@"battery level %f",batteryLevel);
                batteryLevel *= 100;
                int batLevel = (int) batteryLevel;
                batString= [NSString stringWithFormat:@"%i", abs(batLevel)];
            }
            @catch (NSException *exception) {

            }
            @finally {

            }

            NSString* strSaveUserApplicationStatusCalled = [NSString stringWithFormat:@"%i", saveUserApplicationStatusCalled];
            [glbData appendString:dateString];
            [glbData appendString:@"|"];
            [glbData appendString:latitude];
            [glbData appendString:@"|"];
            [glbData appendString:longitude];
            [glbData appendString:@"|"];
            [glbData appendString:batString];
            [glbData appendString:@"|"];
            [glbData appendString:strSaveUserApplicationStatusCalled];
            [glbData appendString:@"|"];


        }
        if (!saveUserApplicationStatusCalled) {
            saveUserApplicationStatusCalled=1;
        }
        [defaults setObject:glbData forKey:@"glbData"];
        [defaults synchronize];
    }
}





-(void)postMULTILocation:(NSString *) strData{

    strUserName= [defaults stringForKey:@"username"];
    strPassword= [defaults stringForKey:@"password"];
    strUrl = [defaults stringForKey:@"url"];

    ///parameters

    AFHTTPRequestOperation *operation =[[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){

        //NSLog(@"%@",[operation responseString]);
        NSLog(@"RESPONSE:\n%@",[operation responseString]);

        NSData* data = [[operation responseString] dataUsingEncoding:NSUTF8StringEncoding];
        data = [data subdataWithRange:NSMakeRange(0, [data length] - 1)];

        NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:data];

        // create and init our delegate
        MultiPostParser *parser = [[MultiPostParser alloc] initXMLParser];


        [nsXmlParser setDelegate:parser];

        // parsing...
        BOOL success = [nsXmlParser parse];


        if([parser responseStr] != nil && [parser responseStr].length>0){

            [self resetDefaults ];
            glbData = [[NSMutableString alloc] init];
            [self.vAktarDelegate didSend:true];


        }else{
            [self.vAktarDelegate didSend:false];
        }

        isUploadingDataNow = false;
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        isUploadingDataNow = false;
        [self.vAktarDelegate didSend:false];
        NSLog(@"failed %@",error.localizedDescription);
    }];
    [operation start];
}

- (void)resetDefaults {

    [defaults removeObjectForKey:@"glbData"];
    [defaults synchronize];
}


- (void)setLocationManager:(CLLocationManager*)locationManager withDistanceFilter:(CLLocationDistance)distanceFilter {

    self.manager.delegate = nil;
    self.manager = locationManager;
    self.manager.pausesLocationUpdatesAutomatically = NO;
    self.manager.delegate = self;
    self.manager.distanceFilter = distanceFilter;
    [self.manager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
}
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{

}

@end

1 个答案:

答案 0 :(得分:0)

您正在使用SignificantLocationChanges监控位置?我认为在这种情况下,distanceFilter和desiredAccuracy的设置值不会起作用。此方法使用单元格塔更改事件。 请发布完整代码。