監聽屏幕喚醒功能(亮屏開門)


最近公司項目有個新要求,需要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

對於申請后台,“將要掛起============”部分到后面打印速度以及次數成倍增加的原因我也並不清楚,希望有大神能指點一下。Demo稍后在GitHub上放出。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM