I am doing one application to lock and unlock a screen. I am using disableKeygaurd and reeanbleKeygaurd to lock and unlock. the below code i used to unlock:
if(mKeyguardLock == null){
mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
mKeyguardLock = mKeyGuardManager.newKeyguardLock("****");
mKeyguardLock.disableKeyguard();
}
and for lock:
if(mKeyguardLock != null){
mKeyguardLock.reenableKeyguard();
mKeyguardLock = null;
}
It is working fine. but problem is when user unexpectedly unlock a screen by dragging keygaurd(without using my app) then my app will not lock the screen for next time.. I want to work my application when user used unlock by dragging keygaurd also. what should I have to do this? Thanks in advance
Try This:
private KeyguardManager mKeyguardManager;
private KeyguardManager.KeyguardLock mKeyguardLock;
private static final String TAG = "ALERTLock";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int flags = getFlagsForVersion();
getWindow().addFlags(flags);
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
}
private int getFlagsForVersion() {
final String possibleFlags[] = new String[] {
"FLAG_SHOW_WHEN_LOCKED",
"FLAG_DISMISS_KEYGUARD",
"FLAG_TURN_SCREEN_ON"
};
int flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
for(String flag:possibleFlags) {
try {
Field field = WindowManager.LayoutParams.class.getField(flag);
int value = field.getInt(null);
flags |= value;
}
catch(NoSuchFieldException e) { }
catch(IllegalAccessException e) { }
}
return flags;
}
private synchronized void enableKeyguard() {
if (mKeyguardLock != null) {
mKeyguardLock.reenableKeyguard();
mKeyguardLock = null;
}
}
private synchronized void disableKeyguard() {
if (mKeyguardLock == null) {
mKeyguardLock = mKeyguardManager.newKeyguardLock(TAG);
mKeyguardLock.disableKeyguard();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
disableKeyguard();
}
@Override
public void onResume() {
super.onResume();
disableKeyguard();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.