后台运行定位,音频,网络电话

大家都知道我们的程序在后台运行的时间是10分钟,10分钟后便会停止。但是像实时定位,播放音频,以及网络电话这些功能我们需要在后台持续运行。那么我们就要进行相应的设置。
下面具体的例子以定位为例
  1. <SPAN style="FONT-FAMILY: 'comic sans ms', sans-serif; COLOR: #008080; FONT-SIZE: small">#import <UIKit/UIKit.h>
  2. #import <CoreLocation/CoreLocation.h>

  3. @interface BackgroundTrackerViewController : UIViewController<CLLocationManagerDelegate>

  4. @property(nonatomic, retain) CLLocationManager *locationManager;
  5. @property(nonatomic, retain) UIButton *startTrackingButton;
  6. @property(nonatomic, retain) UILabel *alertLabel;

  7. - (void)startTracking:(id)sender;
  8. </SPAN>
复制代码
#import "BackgroundTrackerViewController.h"
  1. <SPAN style="FONT-FAMILY: 'comic sans ms', sans-serif; COLOR: #008080; FONT-SIZE: small">

  2. @interface BackgroundTrackerViewController ()

  3. @end

  4. @implementation BackgroundTrackerViewController
  5. @synthesize locationManager,startTrackingButton,alertLabel;

  6. //开始跟踪
  7. - (void)startTracking:(id)sender
  8. {
  9. [locationManager startUpdatingLocation]; 
  10. }


  11. -(void)start:(id)sender
  12. {
  13. // [locationManager startUpdatingLocation]; 
  14. }

  15. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  16. {
  17. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  18. if (self) {
  19. // Custom initialization
  20. }
  21. return self;
  22. }

  23. - (void)viewDidLoad
  24. {
  25. [super viewDidLoad];
  26. // Do any additional setup after loading the view from its nib.

  27. self.view.backgroundColor=[UIColor grayColor];


  28. self.startTrackingButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
  29. startTrackingButton.frame=CGRectMake(0, 200, 100, 50);
  30. [startTrackingButton addTarget:self action:@selector(startTracking:) forControlEvents:UIControlEventTouchUpInside];
  31. [startTrackingButton setTitle:@"startTracking" forState:UIControlStateNormal];
  32. [self.view addSubview:startTrackingButton];


  33. self.alertLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 50)];
  34. self.alertLabel.backgroundColor=[UIColor orangeColor];
  35. self.alertLabel.hidden=YES;
  36. self.alertLabel.text=@"无法找到位置";
  37. [self.view addSubview:alertLabel];


  38. locationManager = [[CLLocationManager alloc] init];
  39. [locationManager setDelegate:self];
  40. //Only applies when in foreground otherwise it is very significant changes
  41. [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];//要求的精确度
  42. }


  43. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
  44. CLLocationCoordinate2D currentCoordinates = newLocation.coordinate;
  45. [alertLabel setText:@"Location Has been found"];
  46. [alertLabel setHidden:NO];
  47. NSLog(@"Entered new Location with the coordinates Latitude: %f Longitude: %f", currentCoordinates.latitude, currentCoordinates.longitude);
  48. }
  49. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  50. NSLog(@"Unable to start location manager. Error:%@", [error description]);
  51. [alertLabel setHidden:NO];
  52. }


  53. - (void)didReceiveMemoryWarning
  54. {
  55. [super didReceiveMemoryWarning];
  56. // Dispose of any resources that can be recreated.
  57. }
  58. </SPAN>
复制代码
- (void)applicationDidEnterBackground: (UIApplication *)application
  1. <SPAN style="FONT-FAMILY: 'comic sans ms', sans-serif; COLOR: #008080; FONT-SIZE: small">{
  2. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  3. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

  4. if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
  5. { //Check if our iOS version supports multitasking I.E iOS 4
  6. if ([[UIDevice currentDevice] isMultitaskingSupported])
  7. { //Check if device supports mulitasking
  8. UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance

  9. __block UIBackgroundTaskIdentifier background_task; //Create a task object

  10. background_task = [application beginBackgroundTaskWithExpirationHandler: ^{
  11. /*
  12. 当应用程序后台停留的时间为0时,会执行下面的操作(应用程序后台停留的时间为600s,可以通过backgroundTimeRemaining查看)
  13. */
  14. [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
  15. background_task = UIBackgroundTaskInvalid; //Set the task to be invalid

  16. //System will be shutting down the app at any point in time now
  17. }];

  18. // Background tasks require you to use asyncrous tasks

  19. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  20. //Perform your tasks that your application requires
  21. NSLog(@"time remain:%f", application.backgroundTimeRemaining); 
  22. [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
  23. background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
  24. });
  25. }
  26. }


  27. }
  28. </SPAN>
复制代码

修改应用的Info.plist 文件,你需要在Info.plist文件中添加UIBackgroundModes字段,该字段的值是应用支持的所有后台模式,是一个数值类型。目前此数 组可以包含“audio”、“location”和“voip”这三个字符串常量.

转自 
http://dxldy.iteye.com/blog/1759599
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值