一般App進入后臺之后,超過了后臺運行時間,便進入了掛起狀態,無法執行代碼,但是內存并沒有清除。
主要用到2個方法:
beginBackgroundTaskWithExpirationHandler://申請后臺,該方法只有在App處于激活時調用才有效。 endBackgroundTask://注銷后臺
后臺執行3分鐘方法:
#import "AppDelegate.h"
@interface AppDelegate ()
{ NSInteger count;}
@property(strong, nonatomic)NSTimer *mTimer;
@property(assign, nonatomic)UIBackgroundTaskIdentifier backIden;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
count=0;
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
_mTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countAction) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_mTimer forMode:NSRunLoopCommonModes]; [self beginTask];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"進入前臺");
[self endBack];
}//計時
-(void)countAction
{
NSLog(@"%li",count++);}//申請后臺
-(void)beginTask
{
NSLog(@"begin=============");
_backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ //在時間到之前會進入這個block,一般是iOS7及以上是3分鐘。按照規范,在這里要手動結束后臺,你不寫也是會結束的(據說會crash) NSLog(@"將要掛起=============");
[self endBack]; }];
}//注銷后臺
-(void)endBack
{
NSLog(@"end=============");
[[UIApplication sharedApplication] endBackgroundTask:_backIden];
_backIden = UIBackgroundTaskInvalid;
}
@end
無限后臺的方法,慎用!因為這個需要申請后臺播放音頻的權限。如果你的應用不是相關應用,AppStore審核可能不會通過。
好了,看方法!
先在info.plist文件里添加如此一條記錄:
343943d3f4c2.png
AppDelegate.m文件
#import "AppDelegate.h"
@interface AppDelegate ()
{
NSInteger count;
}
@property(strong, nonatomic)NSTimer *mTimer;
@property(assign, nonatomic)UIBackgroundTaskIdentifier backIden;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ // Override point for customization after application launch.
count=0;
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
_mTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countAction) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_mTimer forMode:NSRunLoopCommonModes]; [self beginTask];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{ NSLog(@"進入前臺"); [self endBack];
}//計時
-(void)countAction
{
NSLog(@"%li",count++);
}//申請后臺
-(void)beginTask
{
NSLog(@"begin=============");
_backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ NSLog(@"將要掛起=============");
[self endBack]; }];
}//注銷后臺
-(void)endBack{ NSLog(@"end=============");
[[UIApplication sharedApplication] endBackgroundTask:_backIden];
_backIden = UIBackgroundTaskInvalid;
}
@end
ViewController.m文件
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property(strong, nonatomic)AVAudioPlayer *mPlayer;
@property(assign, nonatomic)CGFloat mCount;
@end
@implementation ViewController
- (void)viewDidLoad
{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
_mCount = 0;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(countTime) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)countTime
{ _mCount+=10;
NSLog(@"%f",_mCount);
if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 60.)
{//當剩余時間小于60時,開如播放音樂,并用這個假前臺狀態再次申請后臺 NSLog(@"播放%@",[NSThread currentThread]);
[self playMusic]; //申請后臺
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ NSLog(@"我要掛起了"); }];
}
}
-(void)playMusic
{ //1.音頻文件的url路徑,實際開發中,用無聲音樂
NSURL *url=[[NSBundle mainBundle]URLForResource:@"歡沁.mp3" withExtension:Nil];
//2.創建播放器(注意:一個AVAudioPlayer只能播放一個url)
_mPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//3.緩沖 [_mPlayer prepareToPlay];
//4.播放 [_mPlayer play];
}
@end