0

In Android, you can go to system settings and enable screen locking whereby you can require a password, PIN, or some other means of unlocking the screen. This is typically used if you put your device into standby mode or it goes into standby mode after the screen dims out.

What I would like to do is to re-use this screen locking within my app but to prevent access to a particular activity. I would have a button that when pressed brings up the screen lock activity where the user must enter their PIN. If they enter it correctly, I then let them have access to the activity, otherwise they cannot use it.

Is it possible to re-use the screen locking activity in this scenario? If so, what API do I need? Would be nice if it worked on Android 2.3

EDIT:

Some of you are assuming that my app REQUIRES a PIN or password to operate. That is not the case. Users who want to protect certain data in my app can require it to have a PIN or password in order to view it. But why write my own password/PIN activity or dialog when the system already has one.

Johann
  • 27,536
  • 39
  • 165
  • 279

3 Answers3

0

That kind of security is only available to inbuilt system components, like the settings app. Third party apps cannot request for the password dialog to be shown, and only continue working if the user enters the correct code.

Additionally, a decent amount of users simply do not have a pass code on their device.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Don't see the logic in that. It's just another system component. As for users not having the pass code, that is irrelevant. Users using my app who want to use such a feature, enable it. The purpose of the screen lock should be nothing more than a go/no-go check. There is nothing of security issues involved in displaying the screen lock. – Johann May 11 '13 at 05:40
  • @AndroidDev Whether you see the logic in that or not is irrelevant. You still cannot do it unless you are a system app. There is no API that exposes this functionality. – Raghav Sood May 11 '13 at 05:45
0

You could look at KeyguardManager and KeyguardManager.KeyguardLock. There seems to be a change in how this functionality works starting in API level 13, but I'm not familiar with the topic in general, so you'll have to investigate that if you plan to go this route.

You are probably better off implementing something self contained in your app. It's very easy to cover the screen (perhaps with another Activity) or to hide UI elements programmatically and show an alternate UI with a password input field or something.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • As I posted in my comment to Raghav, it's totally irrelevant that users have passwords or not. If they want to enable a feature in my app that requires the use of a PIN, they enable that. You are assuming that my app REQUIRES a PIN for some feature. That is not the case. My users who want to protect a part of their data can do this by requiring a password or PIN if they so choose. But why should I re-invent the wheel when the system screen locking activity already does everything. – Johann May 11 '13 at 05:47
  • The problem with enabling the Keyguard is it enables it for the *device*, not for your app. You are essentially locking their device. If they want to do anything else, they will have to disable the Keyguard anyway, which makes what you want to conceal visible again. – Karakuri May 11 '13 at 05:50
  • Basically, what you are suggesting is to re-invent the wheel. The screen lock could have been a generic activity re-usable by any app and not used exclusively for locking the OS. It should be used as a go/no-go way of verifying whether the person knows the password or PIN. Clearly this is a design flaw in Android. – Johann May 11 '13 at 05:53
  • @AndroidDev You are free to make that suggestion to Google. In the meantime, it is still not possible and you will have to implement it yourself – Raghav Sood May 11 '13 at 05:54
  • Then you should complain to Google. The `Keyguard` secures the device, and neither you nor I have any way to change this. – Karakuri May 11 '13 at 05:56
-1

You can use the Screen locking feature from Android 2.2 itself.So, it will work in 2.3 easily.The method to use this feature can be done in basically two ways.

1st one is.

There are two way you can lock the screen:

   PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);


    manager.goToSleep(int amountOfTime);

The second one is

     PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,Your        Tag");
      wl.acquire();
     wl.release();

The permission is also needed.that is

  <uses-permission android:name="android.permission.WAKE_LOCK" />
Chiradeep
  • 973
  • 12
  • 32
  • I'm not interested in locking the screen. I believe my post was clear on that. – Johann May 11 '13 at 05:44
  • `PowerManager.goToSleep()` turns the screen off, but the user might not be using password security or even a screen lock at all. `WakeLock`s have nothing to do with the lock screen, they just prevent the device form sleeping. – Karakuri May 11 '13 at 05:46