亲宝软件园·资讯

展开

Android ScreenLockReceiver监听锁屏 Android ScreenLockReceiver监听锁屏功能示例

只要你能好 人气:0
想了解Android ScreenLockReceiver监听锁屏功能示例的相关内容吗,只要你能好在本文为您仔细讲解Android ScreenLockReceiver监听锁屏的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ScreenLockReceiver,监听,锁屏,下面大家一起来学习吧。

本文实例讲述了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程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论