`
吃饱了就饿
  • 浏览: 68122 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • IOS
阅读更多

大家都知道我们的程序在后台运行的时间是10分钟,10分钟后便会停止。但是像实时定位,播放音频,以及网络电话这些功能我们需要在后台持续运行。那么我们就要进行相应的设置。

下面具体的例子以定位为例

 

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface BackgroundTrackerViewController : UIViewController<CLLocationManagerDelegate>

@property(nonatomic, retain) CLLocationManager *locationManager;
@property(nonatomic, retain)  UIButton *startTrackingButton;
@property(nonatomic, retain)  UILabel  *alertLabel;

- (void)startTracking:(id)sender;

 #import "BackgroundTrackerViewController.h"



@interface BackgroundTrackerViewController ()

@end

@implementation BackgroundTrackerViewController
@synthesize locationManager,startTrackingButton,alertLabel;

//开始跟踪
- (void)startTracking:(id)sender
{
    [locationManager startUpdatingLocation]; 
}


-(void)start:(id)sender
{
//    [locationManager startUpdatingLocation]; 
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor=[UIColor grayColor];
    
    
    self.startTrackingButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    startTrackingButton.frame=CGRectMake(0, 200, 100, 50);
    [startTrackingButton addTarget:self action:@selector(startTracking:) forControlEvents:UIControlEventTouchUpInside];
    [startTrackingButton setTitle:@"startTracking" forState:UIControlStateNormal];
    [self.view addSubview:startTrackingButton];
    
    
    self.alertLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 50)];
    self.alertLabel.backgroundColor=[UIColor orangeColor];
    self.alertLabel.hidden=YES;
    self.alertLabel.text=@"无法找到位置";
    [self.view addSubview:alertLabel];
    
    
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    //Only applies when in foreground otherwise it is very significant changes
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];//要求的精确度
}


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


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 - (void)applicationDidEnterBackground:(UIApplication *)application

{
    // 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.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  
    if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
    { //Check if our iOS version supports multitasking I.E iOS 4
		if ([[UIDevice currentDevice] isMultitaskingSupported])
        { //Check if device supports mulitasking
			UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
            
			__block UIBackgroundTaskIdentifier background_task; //Create a task object
            
			background_task = [application beginBackgroundTaskWithExpirationHandler: ^{
                /*
                 当应用程序后台停留的时间为0时,会执行下面的操作(应用程序后台停留的时间为600s,可以通过backgroundTimeRemaining查看)
                 */
				[application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
				background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
                
				//System will be shutting down the app at any point in time now
			}];
            
			// Background tasks require you to use asyncrous tasks
            
			dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
				//Perform your tasks that your application requires
                NSLog(@"time remain:%f", application.backgroundTimeRemaining);                
				[application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
				background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
			});
		}
	}
    
    
}

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

3
5
分享到:
评论

相关推荐

    关于iOS App后台运行的调研报告.pdf

    3. **VoiceoverIP (VoIP)**:网络即时通讯类应用可以通过申请VoIP权限来实现后台运行。 4. **Newsstand downloads**:适用于报刊类应用下载内容。 5. **外部配件通信**:通过MFi认证的外设可通过蓝牙与应用进行通信...

    iOS后台运行的几种方式例子

    5. **持续执行(延长的后台执行)**:尽管iOS不鼓励无限后台运行,但开发者可以通过巧妙设计,如使用定位服务或音频播放来模拟无限后台。然而,Apple对这种行为有严格的审查,过度使用可能会导致应用被拒或被用户...

    IOS应用源码——后台运行(Multitasking)以及本地通知(Local Notifications) zip

    1. **音频播放**:当应用正在播放音乐或音频时,可以继续在后台运行。 2. **定位服务**:应用需要实时更新用户的地理位置时,可以在后台运行。 3. **后台刷新**:应用可以定期在后台获取新数据,如新闻、邮件等。 4....

    IOS应用源码之后台运行(Multitasking)以及本地通知(Local Notifications)Demo .zip

    2. **定位服务**:如果应用需要持续获取用户的位置信息,可以申请后台运行权限。 3. **VoIP服务**:支持VoIP电话的应用可以在后台保持网络连接,以便随时接听电话。 4. **后台刷新**:允许应用定期更新内容,如新闻...

    ios开发实例

    在iOS中,应用程序的后台运行是一个关键特性,它允许用户在执行其他任务时,如接听电话或使用其他应用,仍然可以保持某些功能的运行。苹果提供了几种不同的后台模式,以便开发者根据应用需求选择合适的方式。 1. **...

    太阳通话录音

    本软件需要的权限包括:打开网络接口、读取用户的联系人数据、监督、限定或终止呼出的电话、访问网络上的信息、访问范围(如WIFI)性的定位、记录音频信息、读取电话的状态、访问震动器、写用户的外部存储器;...

    第七单元微机故障检测与定位组装检修高新考试题.doc

    3. 磁盘碎片整理:当磁盘碎片整理总是停止在10%,可能是由于其他程序干扰,需确保没有后台程序运行后再进行碎片整理。 4. 内存故障:内存校验错误通常暗示内存条存在问题,可能是CMOS设置错误或内存条部分损坏。...

    Android毕业设计; Android, Music Player, for graduation.zip

    服务可以在后台运行,即使用户离开应用程序也能继续播放音乐。同时,需要关注服务的生命周期管理和资源优化。 5. **广播接收器**:当用户接听电话或设备屏幕关闭时,可能需要暂停音乐播放。通过注册广播接收器,你...

    基于android的位置闹钟.doc

    - **Service**:后台运行的服务,不与用户界面交互。 - **Notification**:在状态栏显示提醒。 - **Content Provider**:用于共享数据。 ### 第三部分:关键技术和实现 #### 1. Android权限获取 应用程序需要在...

    音乐播放器

    当应用在后台运行时,通过通知栏提供播放控制是一种常见的做法。这需要使用`NotificationCompat.Builder`创建通知,并绑定一个`RemoteViews`来展示和接收用户操作。 9. **权限管理**: Android系统对文件访问和...

    电信设备-导航时通话接入的处理方法及移动终端.zip

    用户可以决定是否接听电话,接听后,导航应用通常会在后台继续运行,但可能需要用户手动恢复到前台。 2. **多任务并行处理**:某些高级设备和操作系统允许在通话的同时运行其他应用,包括导航。这依赖于高效的...

    android架构层次了解及涉及技术归纳

    例如,Activity Manager管理应用生命周期,Content Provider用于数据共享,Intent机制实现组件间的通信,Broadcast Receiver接收全局广播事件,Service则支持后台运行任务。 5. **应用程序层**:用户直接接触的应用...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    Webview WebView(网络视图)能加载显示网页,可以将其视为一个浏览器。它使用了WebKit渲染引擎加载显示网页 Activity Activity是一个应用程序组件,提供一个屏幕,用户可以用来交互为了完成某项任务,是一个负责与...

    VoLTE专项全方位指导书0421.pdf

    测试终端选择的是华为Mate7,GPS用于定位,PC用于运行Probe软件进行测试控制。MOS盒在需要进行MOS(Mean Opinion Score,主观音质评价)测试时使用,它用于评估通话质量。 2. 设备连接的步骤明确指出如何使用HUAWEI...

    android入门经典资料

    - **Service**: 在后台运行的任务,可以独立于任何用户界面存在。 - **Broadcast Receiver**: 用于接收来自系统或其他应用程序的广播消息。 - **Content Provider**: 提供数据存储和访问接口,实现数据共享。 #### ...

    android程序的一个简易的播放器

    服务可以在后台运行,即使应用被最小化或关闭。 7. **事件监听**: - **BroadcastReceiver**: 可用于监听系统广播事件,例如,当耳机插拔时,应用可以通过BroadcastReceiver来响应并改变播放行为。 8. **权限管理...

    生活分类信息发布网站的优秀网站管理系统正式版

    后台可设置信息发布是否需在通过审核、信息发布间隔时间、同一会员允许当天发布信息数量、电话过滤、非法字过滤、会员注册邮箱激活,多重过滤保障了信息的真实性、实用性。上海分类信息网站,地方分类信息网站,天津...

    网软志成分类信息网站系统.net官方商业版

    后台可设置信息发布是否需在通过审核、信息发布间隔时间、同一会员允许当天发布信息数量、电话过滤、非法字过滤、会员注册邮箱激活,多重过滤保障了信息的真实性、实用性。上海分类信息网站,地方分类信息网站,天津...

Global site tag (gtag.js) - Google Analytics