开发者

Android WakeLock and KeyGuard

开发者 https://www.devze.com 2023-01-02 09:51 出处:网络
I\'m just wondering if I do this correctly; I\'m programming a notification app which can display a notification when the phone is sleeping

I'm just wondering if I do this correctly; I'm programming a notification app which can display a notification when the phone is sleeping

  1. Disable keyguard lock
  2. Aquire a wake lock
  3. show notification
  4. Set alarm for timeout and reenabling keyguard and release wakelock is the user dont touches the screen. 4.1 User touches the screen, and I disable the timer. Do nothing more. Done and done 4.2 User dont touch the screen, so reenable keyguard开发者_C百科 and release wakelock. Phone sleeps again

Basically I'm wondering about point 4.1 the most. cancel the pendingintent for the alarm, and do nothing more? or should the keyguard and wakelock that are set be dealt with in some way?


The trick to implement your own Keyguard-replacement appears to be the following:-

  • In the onCreate method, you don't disable the keyguard, but the user can interact with the screen at this point so you need to be careful about accidental touches.

     getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_KEEP_SCREEN_ON);
    
    mKeyguardManager = (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
    mKeyguardLock = mKeyguardManager.newKeyguardLock(TAG);
    
  • If the user performs some action to indicate they want to interact more fully, then we can disable the keyguard and move on.

    mKeyguardLock.disableKeyguard(); mKeyguardManager.exitKeyguardSecurely(null);

  • If they don't, then since the keyguard isn't disabled you shouldn't need to do anything more, just finish your activity

Thats it, but I'm still testing it. So, I'm not 100% sure about it.


I know this question is old but the API clearly state that programatically trying to acquire a keyguard unlock is deprecated.

The correct strategy is, in the oncCreate method of your activity, to have:

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Hope this helps others.


Locks are global and when one appplication acquires it it will be off until the lock is released. You should always reenable locks. Otherwise system won't go sleep or lock from Home or any other application

EDIT: I'm not really shure how it works with keyguard :/

0

精彩评论

暂无评论...
验证码 换一张
取 消