Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Keyguard Lock

I am working on an application that will replace the default lock screen (swipe to unlock) for android devices. I have successfully done this by disabling the keyguard manager and showing my activity using the broadcast receiver for screen OFF and screen ON intent. Now, the problem is when I set the default screen lock again for any reason then my application would not disable the keyguard unless I force close it and launch it again.

      km = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
      if( km.inKeyguardRestrictedInputMode()) {
       //it is locked
          km = (KeyguardManager) getApplicationContext().getSystemService(KEYGUARD_SERVICE);
      kl=km.newKeyguardLock("com.example.helloworld.MainActivity");
      kl.disableKeyguard();
      } else {
          Intent i = getIntent();
          i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          getApplicationContext().startActivity(i);
      }
like image 508
salman Avatar asked Feb 19 '23 19:02

salman


2 Answers

You cannot replace the lock screen with a user application. Anything you do is a hack and may or may not work on some device, and will likely break with new releases. You can create something that looks like a screen lock, but it won't work like one. Additionally, in recent versions of Android (post-ICS), unlocking the screen does extra things like unlocking the credential storage, which your app cannot possibly do (since it doesn't have system permissions).

If you really want to replace the screen lock, you need to build your own Android ROM, modifying/replacing the stock one.

like image 145
Nikolay Elenkov Avatar answered Feb 26 '23 22:02

Nikolay Elenkov


The accepted answer may be out of date.

  • It's now possible to use Device Admin to create and remove device passwords.
  • An application can be placed above the current lock screen using FLAG_SHOW_WHEN_LOCKED (complete explanation in another answer)
  • The keyguard can be dismissed using FLAG_DISMISS_KEYGUARD

As a result, it's theoretically possible to secure the actual lock screen using an app-generated password (providing real security), float a custom lock screen above the android lock screen, and -- when a proper password is provided -- unlock and dismiss the real lock screen. Finally, you would use a receiver to restore or clear the password on relevant events like SCREEN_OFF or SCREEN_ON -- the latter could automatically clear the password if a timeout was not yet reached.

FWIW, I don't recommend this approach since a crash or uninstall would leave a user with a device locked by a password they do not know.

like image 42
claytond Avatar answered Feb 26 '23 22:02

claytond