Integrate Pattern Lock in Android | Techbirds

Posted on: December 5, 2013 /

Categories: Android Apps / Author Name: Md. Sadiq Ali

Steps to integrate patten lock in your app :-

1. Download pattern lock library from here.

2. Import the library in your project.

3. Add LockPatternActivity in AndroidManifest.xml

4. Create new pattern

Add following lines in your activity to launch the pattern activity

private static final int REQ_CREATE_PATTERN = 1;

Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN, null,
        your-context, LockPatternActivity.class);
startActivityForResult(intent, REQ_CREATE_PATTERN);

5. Getting result from the started activity

Override the onActivityResult method in your activity to get result

@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    switch (requestCode) {
    case REQ_CREATE_PATTERN: {
        if (resultCode == RESULT_OK) {
            char[] pattern = data.getCharArrayExtra(
                    LockPatternActivity.EXTRA_PATTERN);
            …
        }
        break;
    }
    }
}

You can save the created pattern in your apps database, preferences or in the pattern library’s preferences (see Notes below).

6. Compare pattern

Add following code in your activity to compare the pattern entered by the user with the saved pattern.

private static final int REQ_ENTER_PATTERN = 2;

char[] savedPattern = …

Intent intent = new Intent(LockPatternActivity.ACTION_COMPARE_PATTERN, null,
        your-context, LockPatternActivity.class);
intent.putExtra(LockPatternActivity.EXTRA_PATTERN, savedPattern);
startActivityForResult(intent, REQ_ENTER_PATTERN);

and in onActivityResult method add

@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    switch (requestCode) {
    case REQ_ENTER_PATTERN: {
        /*
         * NOTE that there are 4 possible result codes!!!
         */
        switch (resultCode) {
        case RESULT_OK:
            // The user passed
            break;
        case RESULT_CANCELED:
            // The user cancelled the task
            break;
        case LockPatternActivity.RESULT_FAILED:
            // The user failed to enter the pattern
            break;
        case LockPatternActivity.RESULT_FORGOT_PATTERN:
            // The user forgot the pattern and invoked your recovery Activity.
            break;
        }

        break;
    }
    }
}

NOTES :

– You can tell the pattern library to store newly created pattern into SharedPreferences by adding following line in your activity

SecurityPrefs.setAutoSavePattern(your-context, true);

– There are four built-in themes which you can apply to LockPatternActivity in AndroidManifest.xml :

Alp_Theme_Dark, Alp_Theme_Light, Alp_Theme_Dialog_Dark and Alp_Theme_Dialog_Light

– You can download the demo app from  Google Play.

For more info click here

Enjoy….

5,771 total views, 1 views today

Share this Onfacebook-6273467twitter-6307282linkedin-3768746google-7654215