How to Set Up Screen Lock with a Password Programmatically

Programmatically changing ScreenLock mode from Slide to Pattern Android

You can use Device Administration API. And DeviceAdminSample is a good start.

There are following types you can set using setPasswordQuality()

        DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED,
DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
DevicePolicyManager.PASSWORD_QUALITY_NUMERIC,
DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC,
DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC,
DevicePolicyManager.PASSWORD_QUALITY_COMPLEX

There are several answers on SO

  • How can i set up screen lock with a password programmatically?

  • How can i lock the android device with a password programmatically

How to Prompt User to Enter Pin in Android Lock Screen?

You can use the below code to open password/pin/pattern screen and validate user device credentials:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

if (km.isKeyguardSecure()) {
Intent authIntent = km.createConfirmDeviceCredentialIntent(getString(R.string.dialog_title_auth), getString(R.string.dialog_msg_auth));
startActivityForResult(authIntent, INTENT_AUTHENTICATE);
}
}

and also implement onActivityResult's method to get the results in your case is successful or not.

// call back when password is correct  
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == INTENT_AUTHENTICATE) {
if (resultCode == RESULT_OK) {
//do something you want when pass the security
}
}
}

You can check ref URL from here

Use screen lock in my app

Actually, there is an API to exactly that using the KeyguardManager.

First get a the Keyguard SystemService:

KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);

And then request an authentication intent using:

Intent i = km.createConfirmDeviceCredentialIntent(title,description);

start this intent using startActivityForResult(Intent, int) and check for RESULT_OK if the user successfully completes the challenge.

This is for API level 21.
Previous versions might work with KeyguardLock.

How to programmatically set a lock or pin for an app

Logic

  • You have to make and start a service when you want to block apps,
  • And In Service you have to check packagenames of the apps, so that you can decide which app to run and which to show a pin/password activity

Now Code Example:

  • To Start a service, code like this,

    startService(new Intent(this, SaveMyAppsService.class));
  • Now, Inside your service, check packages like this,

    public class SaveMyAppsService extends android.app.Service 
    {

    String CURRENT_PACKAGE_NAME = {your this app packagename};
    String lastAppPN = "";
    boolean noDelay = false;
    public static SaveMyAppsService instance;

    @Override
    public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

    scheduleMethod();
    CURRENT_PACKAGE_NAME = getApplicationContext().getPackageName();
    Log.e("Current PN", "" + CURRENT_PACKAGE_NAME);

    instance = this;

    return START_STICKY;
    }

    private void scheduleMethod() {
    // TODO Auto-generated method stub

    ScheduledExecutorService scheduler = Executors
    .newSingleThreadScheduledExecutor();
    scheduler.scheduleAtFixedRate(new Runnable() {

    @Override
    public void run() {
    // TODO Auto-generated method stub

    // This method will check for the Running apps after every 100ms
    if(30 minutes spent){
    stop();
    }else{
    checkRunningApps();
    }
    }
    }, 0, 100, TimeUnit.MILLISECONDS);
    }

    public void checkRunningApps() {
    ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
    ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
    String activityOnTop = ar.topActivity.getPackageName();
    Log.e("activity on TOp", "" + activityOnTop);

    // Provide the packagename(s) of apps here, you want to show password activity
    if (activityOnTop.contains("whatsapp") // you can make this check even better
    || activityOnTop.contains(CURRENT_PACKAGE_NAME)) {
    // Show Password Activity
    } else {
    // DO nothing
    }
    }

    public static void stop() {
    if (instance != null) {
    instance.stopSelf();
    }
    }
    }

Edit: (Get Top Package Name for Lollipop)

A very good answer is here.

android, Open lock screen settings programmatically

Possible duplicate of this SO Post

Have you tried doing this to navigate the user to a specific Settings Page -

Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
startActivity(intent);

For opening the Security Settings use this -

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS); 
startActivity(intent);

Moreover you can find here the constants for all the possible Actions that you can use in your app to redirect the user to a particular page on the Setting screen.

Let me know if this is not what you were looking for.



Related Topics



Leave a reply



Submit