最近公司项目有个新要求,需要APP常驻后台,并能在用户唤醒屏幕(未解锁状态下)监听此操作,并实现APP中的自动开门功能。整理一下自己的思路做法,希望帮到有需要的朋友。
首先我们要用到 <notify.h>,这个头文件里面提供了用于进程之间的无状态通知方法。用法和我们通知使用差不多。其次为了满足常驻后台功能,这里用实现 AVFoundation后台播放。这里为了防止亮屏方法被多次调用,后来又增加了自动锁屏功能,在完成亮屏开门功能完成后自动锁屏。
//自动锁屏
[UIApplication sharedApplication].idleTimerDisabled=NO;
状态设置
当我们第一次注册某个通知时候,可能并不知道当前资源是否可以使用,必须等待通知的回调。系统也提供了一个解决方法,如果是发送方,在资源可以使用的时候做一个标记位,接受方,在注册之前可以先检查下,当前资源是否可以使用,如果可以使用,可以直接进入自己的逻辑处理。以下是为监听功能的注册。
//监听锁屏状态 lock=1则为锁屏状态
uint64_t locked;
__block int token = 0;
notify_register_dispatch("com.apple.springboard.lockstate",&token,dispatch_get_main_queue(),^(int t){
});
notify_get_state(token, &locked);
//监听屏幕点亮状态 screenLight=1则为变暗关闭状态
uint64_t screenLight;
__block int lightToken = 0;
notify_register_dispatch("com.apple.springboard.hasBlankedScreen",&lightToken,dispatch_get_main_queue(),^(int t){
});
notify_get_state(lightToken, &screenLight);
其实<notify.h>还提供了很多系统事件可用于监听,比如锁屏或者低电量、充电状态等。
奉上链接 http://iphonedevwiki.net/index.php/SpringBoard.app/Notifications
下面上完整代码
首先在AppDelegate.m中实现
#import "AppDelegate.h"
#define NotificationLockCFSTR ("com.apple.springboard.lockcomplete")
#define NotificationChangeCFSTR ("com.apple.springboard.lockstate")
#define NotificationPwdUICFSTR ("com.apple.springboard.hasBlankedScreen")
@interface AppDelegate ()
{
NSInteger count;
}
@property(strong, nonatomic) NSTimer *mTimer;
@property(assign, nonatomic) UIBackgroundTaskIdentifier backIden;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
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;
}
然后在ViewController.m中实现以下
#import "ViewController.h"
#import <notify.h>
#import <AVFoundation/AVFoundation.h>
#define NotificationLockCFSTR ("com.apple.springboard.lockcomplete")
#define NotificationChangeCFSTR ("com.apple.springboard.lockstate")
#define NotificationPwdUICFSTR ("com.apple.springboard.hasBlankedScreen")
@interface ViewController ()
@property(nonatomic,weak) NSTimer *timer;
@property(strong, nonatomic)AVAudioPlayer *mPlayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 60.) {//当剩余时间小于60时,开如播放音乐,并用这个假前台状态再次申请后台
NSLog(@"播放%@",[NSThread currentThread]);
[self playMusic];
//申请后台
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"即将挂起");
}];
}
//监听锁屏状态 lock=1则为锁屏状态
uint64_t locked;
__block int token = 0;
notify_register_dispatch("com.apple.springboard.lockstate",&token,dispatch_get_main_queue(),^(int t){
});
notify_get_state(token, &locked);
//监听屏幕点亮状态 screenLight=1则为变暗关闭状态
uint64_t screenLight;
__block int lightToken = 0;
notify_register_dispatch("com.apple.springboard.hasBlankedScreen",&lightToken,dispatch_get_main_queue(),^(int t){
});
notify_get_state(lightToken, &screenLight);
// NSLog(@"screenLight=%llu locked=%llu",screenLight,locked);
if (screenLight == 1 || locked == 0) {
NSLog(@"screenLight = %llu,locked = %llu",screenLight,locked);
NSLog(@"------");
return;
}else{
NSLog(@"screenLight = %llu,locked = %llu",screenLight,locked);
NSLog(@"检测到亮屏========");
//自动锁屏
[UIApplication sharedApplication].idleTimerDisabled=NO;
return;
}
}];
_timer = timer;
NSRunLoop *loop = [NSRunLoop currentRunLoop];
[loop addTimer:timer forMode:NSRunLoopCommonModes];
}
-(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];
}
-(void)dealloc{
[_timer invalidate];
_timer = nil;
}
@end
对于申请后台,“将要挂起============”部分到后面打印速度以及次数成倍增加的原因我也并不清楚,希望有大神能指点一下。