How to Detect Android pattern lock Events.
Step 1: Create a android project with android studio.
Step 2 : Create a class "AdminReceiver" and put the following code into the class.
Step3: Into the main activity class put the following code into your oncreate method.
Step4 : Your manifest should be look like this add the broadcast intent to receive the pattern.
You can download the sample code from here
Step 1: Create a android project with android studio.
Step 2 : Create a class "AdminReceiver" and put the following code into the class.
public class AdminReceiver extends DeviceAdminReceiver {
@Override public void onEnabled(Context ctxt, Intent intent) {
ComponentName cn=new ComponentName(ctxt, AdminReceiver.class);
DevicePolicyManager mgr=
(DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
mgr.setPasswordQuality(cn,
DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC);
onPasswordChanged(ctxt, intent);
}
@Override public void onPasswordChanged(Context ctxt, Intent intent) {
DevicePolicyManager mgr=
(DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
int msgId;
if (mgr.isActivePasswordSufficient()) {
msgId=R.string.compliant;
}
else {
msgId=R.string.not_compliant;
}
Toast.makeText(ctxt, msgId, Toast.LENGTH_LONG).show();
}
@Override public void onPasswordFailed(Context ctxt, Intent intent) {
Toast.makeText(ctxt, R.string.password_failed, Toast.LENGTH_LONG)
.show();
}
@Override public void onPasswordSucceeded(Context ctxt, Intent intent) {
Toast.makeText(ctxt, R.string.password_success, Toast.LENGTH_LONG)
.show();
}
}
Step3: Into the main activity class put the following code into your oncreate method.
ComponentName cn=new ComponentName(this, AdminReceiver.class);This code will get the admin permission from the user and when user gives the permission the service will run.
DevicePolicyManager mgr=
(DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
if (mgr.isAdminActive(cn)) {
int msgId;
if (mgr.isActivePasswordSufficient()) {
msgId=R.string.compliant;
}
else {
msgId=R.string.not_compliant;
}
Toast.makeText(this, msgId, Toast.LENGTH_LONG).show();
}
else {
Intent intent=
new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getString(R.string.device_admin_explanation));
startActivity(intent);
}
finish();
}
Step4 : Your manifest should be look like this add the broadcast intent to receive the pattern.
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.commonsware.android.pwenforce" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="18"/>
<uses-feature android:name="android.software.device_admin" android:required="true"/>
<application android:allowBackup="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name="MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="AdminReceiver" android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
<action android:name="android.app.action.ACTION_PASSWORD_CHANGED"/>
<action android:name="android.app.action.ACTION_PASSWORD_FAILED"/>
<action android:name="android.app.action.ACTION_PASSWORD_SUCCEEDED"/>
</intent-filter>
</receiver>
</application>
</manifest>
You can download the sample code from here

No comments:
Post a Comment