应用锁屏 /解锁 操作监听

相关宏定义:

#define NotificationLock CFSTR("com.apple.springboard.lockcomplete")

#define NotificationChange CFSTR("com.apple.springboard.lockstate")

#define NotificationPwdUI CFSTR("com.apple.springboard.hasBlankedScreen")

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [self addScreenStatusObserve];  // 视频锁屏监听

    /*

     应用程序自身代理的方法直接写需求即可。方法如下:

    

     // 进入前台

    1. - (void)applicationWillEnterForeground:(UIApplication *)application

    

     // 非活动状态,会在后台保持一段时间,再将程序杀死

    2. - (void)applicationDidBecomeActive:(UIApplication *)application

 

     */

    return YES;

}

 

 

- (void)addScreenStatusObserve {

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationLock, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);

    

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged,  NotificationChange, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);

}

 

 

static void screenLockStateChanged(CFNotificationCenterRef center,void* observer,CFStringRef name,const void* object,CFDictionaryRef userInfo)

{

    

    NSString* lockstate = (__bridge NSString*)name;

    

    if ([lockstate isEqualToString:(__bridge  NSString*)NotificationLock]) {

        

        NSLog(@"locked.");

        

    } else if ([lockstate isEqualToString:(__bridge  NSString*)NotificationChange]) {

        

        NSLog(@"lock state changed.");

        

    }

    

}

 

转载于:https://www.cnblogs.com/tony0571/p/6121654.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现当当前是锁屏状态时按任意遥控键即解锁,可以使用以下代码: 1. 首先,在AndroidManifest.xml文件中添加以下权限: <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> 2. 在你的Activity中添加以下代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取设备管理器 DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); // 获取组件名 ComponentName componentName = new ComponentName(this, MyAdminReceiver.class); // 判断是否已经开启了设备管理器 boolean active = devicePolicyManager.isAdminActive(componentName); if (!active) { // 如果没有开启,则打开设备管理器设置页面 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "请开启设备管理器权限"); startActivityForResult(intent, 0); } else { // 如果已经开启,则开始监听按键事件 startLockTask(); } } // 按键事件监听 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_UP: case KeyEvent.KEYCODE_DPAD_DOWN: case KeyEvent.KEYCODE_DPAD_LEFT: case KeyEvent.KEYCODE_DPAD_RIGHT: case KeyEvent.KEYCODE_ENTER: case KeyEvent.KEYCODE_BACK: // 解锁屏幕 DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); devicePolicyManager.lockNow(); return true; } return super.onKeyDown(keyCode, event); } 3. 最后,在你的应用中添加一个设备管理器接收器(MyAdminReceiver),该接收器用于接收设备管理器的一些状态变化事件。在接收器中添加以下代码: public class MyAdminReceiver extends DeviceAdminReceiver { @Override public void onEnabled(Context context, Intent intent) { // 设备管理器开启时启用锁屏任务 ((Activity) context).startLockTask(); } @Override public void onDisabled(Context context, Intent intent) { // 设备管理器关闭时停止锁屏任务 ((Activity) context).stopLockTask(); } } 这样,当你的应用开启后,如果设备管理器没有开启,则会跳转到设置页面进行开启;如果设备管理器已经开启,则可以任意按键解锁屏幕。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值