当前位置:主页 > 移动开发 > Android代码 >

Android ScreenLockReceiver监听锁屏功能示例

时间:2020-10-16 12:45:42 | 栏目:Android代码 | 点击:

本文实例讲述了Android ScreenLockReceiver监听锁屏功能。分享给大家供大家参考,具体如下:

监听屏幕锁屏状态(注册接受者--执行业务--注销接受者)

public class AppLockService extends Service {
  private ActivityManager am;
  private KeyguardManager keyguardManager;
  private LockScreenReceiver receiver;
  @Override
  public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    //注册锁屏广播接收者
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    receiver = new LockScreenReceiver();
    registerReceiver(receiver, filter);
    new Thread(){
      public void run() {
        //一直在监听锁屏动作,一旦监听到,便执行操作
        while(true){
          boolean isLockScreen = keyguardManager.inKeyguardRestrictedInputMode();
          /**此处可作相应操作*/
        }
      };
    }.start();
  }
  private final class LockScreenReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
      tempAppLocks.clear();
    }
  }
  @Override
  public void onDestroy() {
    super.onDestroy();
    //取消锁屏的广播监听
    unregisterReceiver(receiver);
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

您可能感兴趣的文章:

相关文章