RKKeepAlive.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // RKKeepAlive.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2020/7/22.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "RKKeepAlive.h"
  9. #import "AppDelegate.h"
  10. static RKKeepAlive *_keepInstance = nil;
  11. @interface RKKeepAlive(){
  12. int vvvv;
  13. NSTimer *_timer;
  14. }
  15. @property (nonatomic, assign) UIBackgroundTaskIdentifier backgroundTaskIdentifier;
  16. @end
  17. @implementation RKKeepAlive
  18. +(instancetype)sharedKeepInstance{
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. _keepInstance = [[super allocWithZone:NULL]init];
  22. });
  23. return _keepInstance;
  24. }
  25. + (instancetype)allocWithZone:(struct _NSZone *)zone{
  26. return [self sharedKeepInstance];
  27. }
  28. - (void)initPlayer {
  29. [self.player prepareToPlay];
  30. }
  31. - (AVAudioPlayer *)player {
  32. if (!_player) {
  33. NSError *error = nil;
  34. NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"mute-mp3" withExtension:@"mp3"];
  35. AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
  36. audioPlayer.numberOfLoops = NSUIntegerMax;
  37. _player = audioPlayer;
  38. NSLog(@"%s 初始化==:%@",__FUNCTION__,error);
  39. }
  40. return _player;
  41. }
  42. -(void)startAppLifeCycleMonitor{
  43. self.needKeepAliveInBackground = YES;
  44. YBWeakSelf;
  45. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  46. appDelegate.lifeCycleEvent = ^(YBAppLifeCycle lifeCycleType) {
  47. [weakSelf appDelegateBlockEvent:lifeCycleType];
  48. };
  49. }
  50. -(void)appDelegateBlockEvent:(YBAppLifeCycle)lifeCycleType {
  51. switch (lifeCycleType) {
  52. case APPLifeCycle_EnterForeground:{
  53. //[self appActive];
  54. //前台
  55. NSLog(@"%s:应用将进入前台WillEnterForeground", __FUNCTION__);
  56. if (self.needKeepAliveInBackground) {
  57. [self.player pause];
  58. }
  59. [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
  60. }break;
  61. case APPLifeCycle_EnterBackground:{
  62. //[self backGround];
  63. //后台
  64. self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithName:kBgTaskName expirationHandler:^{
  65. if (self.needKeepAliveInBackground) {
  66. [self.player play];
  67. }
  68. if (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
  69. [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
  70. self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
  71. }
  72. }];
  73. NSLog(@"%s:应用进入后台DidEnterBackground", __FUNCTION__);
  74. }break;
  75. case APPLifeCycle_WillTerminate:{
  76. //杀进程
  77. NSLog(@"%s:应用终止:WillTerminate", __FUNCTION__);
  78. }break;
  79. default:
  80. break;
  81. }
  82. }
  83. #pragma mark - 保活测试
  84. -(void)backGround {
  85. [self setupTimer];
  86. }
  87. -(void)appActive {
  88. if (_timer) {
  89. [_timer invalidate];
  90. _timer = nil;
  91. vvvv = 0;
  92. }
  93. }
  94. #pragma mark - 定时器
  95. - (void)setupTimer {
  96. vvvv = 1;
  97. _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerEvent:) userInfo:nil repeats:YES];
  98. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  99. [_timer fire];
  100. }
  101. - (void)timerEvent:(id)sender {
  102. vvvv ++;
  103. NSLog(@"普通定时器运行中:%d",vvvv);
  104. if (vvvv%60 == 0) {
  105. [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%d",vvvv] forKey:@"rk_keep_alive_time"];
  106. }
  107. }
  108. @end