iOS 監(jiān)聽屏幕喚醒功能(亮屏開門)

最近公司項目有個新要求,需要APP常駐后臺,并能在用戶喚醒屏幕(未解鎖狀態(tài)下)監(jiān)聽此操作,并實現(xiàn)APP中的自動開門功能。整理一下自己的思路做法,希望幫到有需要的朋友。

lightScreen.gif

首先我們要用到 <notify.h>,這個頭文件里面提供了用于進程之間的無狀態(tài)通知方法。用法和我們通知使用差不多。其次為了滿足常駐后臺功能,這里用實現(xiàn) AVFoundation后臺播放。這里為了防止亮屏方法被多次調(diào)用,后來又增加了自動鎖屏功能,在完成亮屏開門功能完成后自動鎖屏。

//自動鎖屏
[UIApplication sharedApplication].idleTimerDisabled=NO;

狀態(tài)設(shè)置
當我們第一次注冊某個通知時候,可能并不知道當前資源是否可以使用,必須等待通知的回調(diào)。系統(tǒng)也提供了一個解決方法,如果是發(fā)送方,在資源可以使用的時候做一個標記位,接受方,在注冊之前可以先檢查下,當前資源是否可以使用,如果可以使用,可以直接進入自己的邏輯處理。以下是為監(jiān)聽功能的注冊。

//監(jiān)聽鎖屏狀態(tài) lock=1則為鎖屏狀態(tài)
         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);

//監(jiān)聽屏幕點亮狀態(tài) screenLight=1則為變暗關(guān)閉狀態(tài)
    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>還提供了很多系統(tǒng)事件可用于監(jiān)聽,比如鎖屏或者低電量、充電狀態(tài)等。

Snip20170525_4.png

奉上鏈接 http://iphonedevwiki.net/index.php/SpringBoard.app/Notifications

下面上完整代碼
首先在AppDelegate.m中實現(xiàn)

#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中實現(xiàn)以下

#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時,開如播放音樂,并用這個假前臺狀態(tài)再次申請后臺
        NSLog(@"播放%@",[NSThread currentThread]);
        [self playMusic];
        //申請后臺
        [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            NSLog(@"即將掛起");
        }];
    }

    //監(jiān)聽鎖屏狀態(tài) lock=1則為鎖屏狀態(tài)
    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);

    //監(jiān)聽屏幕點亮狀態(tài) screenLight=1則為變暗關(guān)閉狀態(tài)
    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路徑,實際開發(fā)中,用無聲音樂
NSURL *url=[[NSBundle mainBundle]URLForResource:@"薛之謙-紳士.mp3" withExtension:Nil];

//2.創(chuàng)建播放器(注意:一個AVAudioPlayer只能播放一個url)
_mPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];

//3.緩沖
[_mPlayer prepareToPlay];

//4.播放
[_mPlayer play];
}

-(void)dealloc{
[_timer invalidate];
_timer = nil;
}

@end

對于申請后臺,“將要掛起============”部分到后面打印速度以及次數(shù)成倍增加的原因我也并不清楚,希望有大神能指點一下。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容