国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS實現后臺長時間運行

2019-10-21 18:39:32
字體:
來源:轉載
供稿:網友

前言

一般APP在按下Home鍵被掛起后,這時APP的 backgroundTimeRemaining 也就是后臺運行時間大約只有3分鐘,如果在退出APP后,過十幾二十二分鐘或者更長時間再回到APP,APP就會回到剛打開時的狀態,也就是首頁;有的項目在被掛起后需要在后臺運行一段時間,使有足夠的時間來完成與服務器對接的操作,或者需要一直運行的需求;如果需要,則在APP被掛起后,申請后臺,來延長后臺運行時間。

APP申請后臺運行的方式有幾種:

播放音樂

定位

Newsstand downloads

fetch 等;

iOS,后臺,長時間運行

這里主要說下后臺播放無聲音樂(其實是不播放),采用哪種方式,對應勾選上圖;我的項目中有音頻播放需求,如果沒有,那就找一個播放音頻的理由,或者用其他方式實現。

實現

這里采用了兩個單例:電話監控(XKTelManager)、后臺運行(XKBGRunManager),電話監控可以忽略,視情況而用;采用單例是為了方便管理;

XKTelManager.h

#import <Foundation/Foundation.h>@interface XKTelManager : NSObject///是否在后臺運行@property (nonatomic,assign) BOOL inBackgroundRun;+ (XKTelManager *)sharedManager;/** 來電監聽 */- (void)startMonitor;@end

XKTelManager.m

#import "XKTelManager.h"#import "XKBGRunManager.h"#import <CoreTelephony/CTCallCenter.h>#import <CoreTelephony/CTCall.h>static XKTelManager *_sharedManger;@interface XKTelManager()@property (nonatomic, strong) CTCallCenter *callCenter;@end@implementation XKTelManager+ (XKTelManager *)sharedManager{  static dispatch_once_t onceTelSingle;  dispatch_once(&onceTelSingle, ^{    if (!_sharedManger) {      _sharedManger = [[XKTelManager alloc]init];    }  });  return _sharedManger;}- (instancetype)init{  self = [super init];  if (self) {    _inBackgroundRun = NO;  }  return self;}#pragma mark -********* 監聽電話相關- (void)startMonitor {  __weak typeof(self) weakSelf = self;  _callCenter = [[CTCallCenter alloc] init];  _callCenter.callEventHandler = ^(CTCall * call) {    ///如果已經進入后臺了,不做任何操作    if (weakSelf.inBackgroundRun) {      return;    }    ///APP未進入后臺    if ([call.callState isEqualToString:CTCallStateDisconnected]){      NSLog(@"電話 --- 斷開連接");      [[XKBGRunManager sharedManager] stopBGRun];    }    else if ([call.callState isEqualToString:CTCallStateConnected]){      NSLog(@"電話 --- 接通");    }    else if ([call.callState isEqualToString:CTCallStateIncoming]){      NSLog(@"電話 --- 待接通");      [[XKBGRunManager sharedManager] startBGRun];    }    else if ([call.callState isEqualToString:CTCallStateDialing]){      NSLog(@"電話 --- 撥號中");      [[XKBGRunManager sharedManager] startBGRun];    }    else {      NSLog(@"電話 --- 無操作");    }      };}@end

XKBGRunManager.h

#import <Foundation/Foundation.h>@interface XKBGRunManager : NSObject+ (XKBGRunManager *)sharedManager;/** 開啟后臺運行 */- (void)startBGRun;/** 關閉后臺運行 */- (void)stopBGRun;@end

XKBGRunManager.m

 

#import "XKBGRunManager.h"///循環時間static NSInteger _circulaDuration = 60;static XKBGRunManager *_sharedManger;@interface XKBGRunManager()@property (nonatomic,assign) UIBackgroundTaskIdentifier task;///后臺播放@property (nonatomic,strong) AVAudioPlayer *playerBack;@property (nonatomic, strong) NSTimer *timerAD;///用來打印測試@property (nonatomic, strong) NSTimer *timerLog;@property (nonatomic,assign) NSInteger count;@end@implementation XKBGRunManager{  CFRunLoopRef _runloopRef;  dispatch_queue_t _queue;}+ (XKBGRunManager *)sharedManager{  static dispatch_once_t onceRunSingle;  dispatch_once(&onceRunSingle, ^{    if (!_sharedManger) {      _sharedManger = [[XKBGRunManager alloc]init];    }  });  return _sharedManger;}/// 重寫init方法,初始化音樂文件- (instancetype)init {  if (self = [super init]) {    [self setupAudioSession];    _queue = dispatch_queue_create("com.audio.inBackground", NULL);    //靜音文件    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"****" ofType:@"mp3"];    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];    self.playerBack = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];    [self.playerBack prepareToPlay];    // 0.0~1.0,默認為1.0    self.playerBack.volume = 0.01;    // 循環播放    self.playerBack.numberOfLoops = -1;  }  return self;}- (void)setupAudioSession {  // 新建AudioSession會話  AVAudioSession *audioSession = [AVAudioSession sharedInstance];  // 設置后臺播放  NSError *error = nil;  [audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];  if (error) {    NSLog(@"Error setCategory AVAudioSession: %@", error);  }  NSLog(@"%d", audioSession.isOtherAudioPlaying);  NSError *activeSetError = nil;  // 啟動AudioSession,如果一個前臺app正在播放音頻則可能會啟動失敗  [audioSession setActive:YES error:&activeSetError];  if (activeSetError) {    NSLog(@"Error activating AVAudioSession: %@", activeSetError);  }}/** 啟動后臺運行 */- (void)startBGRun{  [self.playerBack play];  [self applyforBackgroundTask];  ///確保兩個定時器同時進行  dispatch_async(_queue, ^{    self.timerLog = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:1 target:self selector:@selector(log) userInfo:nil repeats:YES];    self.timerAD = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:_circulaDuration target:self selector:@selector(startAudioPlay) userInfo:nil repeats:YES];    _runloopRef = CFRunLoopGetCurrent();    [[NSRunLoop currentRunLoop] addTimer:self.timerAD forMode:NSDefaultRunLoopMode];    [[NSRunLoop currentRunLoop] addTimer:self.timerLog forMode:NSDefaultRunLoopMode];    CFRunLoopRun();  });}/** 申請后臺 */- (void)applyforBackgroundTask{  _task =[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{    dispatch_async(dispatch_get_main_queue(), ^{      [[UIApplication sharedApplication] endBackgroundTask:_task];      _task = UIBackgroundTaskInvalid;    });  }];}/** 打印 */- (void)log{  _count = _count + 1;  NSLog(@"_count = %ld",_count)}/** 檢測后臺運行時間 */- (void)startAudioPlay{  _count = 0;  dispatch_async(dispatch_get_main_queue(), ^{    if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 61.0) {      NSLog(@"后臺快被殺死了");      [self.playerBack play];      [self applyforBackgroundTask];    }    else{      NSLog(@"后臺繼續活躍呢");    }///再次執行播放器停止,后臺一直不會播放音樂文件    [self.playerBack stop];  });}/** 停止后臺運行 */- (void)stopBGRun{  if (self.timerAD) {    CFRunLoopStop(_runloopRef);    [self.timerLog invalidate];    self.timerLog = nil;    // 關閉定時器即可    [self.timerAD invalidate];    self.timerAD = nil;    [self.playerBack stop];  }  if (_task) {    [[UIApplication sharedApplication] endBackgroundTask:_task];    _task = UIBackgroundTaskInvalid;  }}@end

最后在 AppDelegate.m 對應的方法中,實現開啟和停止后臺運行即可!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凤阳县| 平山县| 禄劝| 洮南市| 象山县| 星子县| 上蔡县| 枞阳县| 汉沽区| 嘉义市| 固安县| 拉萨市| 宁津县| 游戏| 浑源县| 仪征市| 龙门县| 土默特右旗| 德惠市| 来安县| 通州区| 镇原县| 马尔康县| 德安县| 铜梁县| 武汉市| 图们市| 吴江市| 中西区| 阿拉尔市| 鄢陵县| 天水市| 邹平县| 枞阳县| 静宁县| 诸城市| 深水埗区| 阜新| 河源市| 宝应县| 五莲县|