Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

android - Enable or disable the PatternLock screen from code

I have tried to find a way to disable the PatternLock screen temporarily. I don't want the lock to be disabled completely, but the user should not need to re-enter his pattern all the time.

My idea is to write a service which disables the pattern after some user activity and re-enables it after a while. (and even more)

There are apps on the market that do something like that (i.e. AutoLock or TogglePattern), so there must be a solution.

I know that I can prevent a lock completely by using:

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

or

KeyguardLock.disableKeyguard()

But that is not what I'm after.

I saw the class com.android.internal.widget.LockPatternUtils in the android sources which is used by the settings activity, but this class is not (at least as far as I know) accessible by a "normal" application.

Do you have any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As of 2.0 (API level 5), you can use this window flag to prevent the lock screen from being displayed while your window is shown:

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED

You can also use this flag to allow a non-secure keyguard to be dismissed when your window is displayed:

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD

Note that these do not allow you to bypass the lock screen outside of your application's environment, which is an intentional design decision.

There is also an older API that lets you hide the lock screen in a similar way to a wake lock:

http://developer.android.com/reference/android/app/KeyguardManager.html#newKeyguardLock(java.lang.String)

Use of this API is discouraged on newer platforms, because it is very easy to get wrong and cause bad behavior (the screen not locking when the user would expect it to), and basically impossible to have clean transitions between activities with unlocked states. For example, this is the API that the in-call screen originally used to hide the lock screen when it was displayed, but as of 2.0 it has switched to the new cleaner window flags. Likewise for the alarm clock etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...