8

I am building a security app and I need to know if the user is giving incorrect password . Suppose user phone is locked by pattern lock system , and unfortunately user has forgotten the pattern password.When user give wrong pattern 5 time, there will be a penalty for 30 sec . I need to catch that penalty event . In my app, i have to do some task (for the safety of user) when this even come . Please help me,

user2166895
  • 81
  • 1
  • 1
  • 7

3 Answers3

14

You can set up a DeviceAdminReceiver that will be notified about failed password attempts, as well as a successful password attempt that occurred after a failed attempt. This is covered in the documentation of Android's device administration APIs.

Note that the user will have to agree to allow your app to serve as a device administrator, via the Settings app, before you will get these events.

This sample project demonstrates listening for those events, plus setting up a password quality policy. The key pieces are:

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I saw the code, this code introduce a new customize password system, i do not need the whole system , i just need to know that the user has entered a wrong password in his default lock system . Can you help on that. Anyway thanks for answering – user2166895 Apr 05 '14 at 17:33
  • @user2166895: "this code introduce a new customize password system" -- no, it does not. "i just need to know that the user has entered a wrong password in his default lock system . Can you help on that" -- that is covered in my answer. – CommonsWare Apr 05 '14 at 17:38
  • But the code disable the pattern , slide or pin lock system, i do not want that . – user2166895 Apr 05 '14 at 17:48
  • @user2166895: "But the code disable the pattern , slide or pin lock system" -- I presume that you are referring to the `setPasswordQuality()` call in `onEnabled()`. You are welcome to completely remove the `onEnabled()` method in `AdminReceiver`, along with the corresponding action in the ``. That is not necessary to receive password events. However, it does not "introduce a new customize password system". – CommonsWare Apr 05 '14 at 17:53
  • Thanks , I got it. But can i get the number of user attempt to access.I mean the number of error .Any code that i should add on this method- ` @Override public void onPasswordFailed(Context ctxt, Intent intent) { Toast.makeText(ctxt, "You will never break into this device!", Toast.LENGTH_LONG) .show(); String tag="tag"; Log.d(tag, "****************This message from Error**************"); }` Thanks again . – user2166895 Apr 05 '14 at 18:46
  • @user2166895: "But can i get the number of user attempt to access.I mean the number of error" -- you will have to track that yourself. – CommonsWare Apr 05 '14 at 18:48
  • Here you are using "minSdkVersion=11", can it be little lower? – user2166895 Apr 05 '14 at 20:20
  • @user2166895: The device admin APIs were added in API Level 8, and the password failed and succeeded actions existed then, so in theory your specific bit should work back to API Level 8. Note that other aspects of the device admin APIs were added later, so check the documentation to see what is and is not available on your desired API level. – CommonsWare Apr 05 '14 at 20:26
  • Thanks . It work on API Level 8. Anyway i get what i wanted . – user2166895 Apr 06 '14 at 03:08
  • DevicePolicyManager policyManager = (DevicePolicyManager) ctx.getSystemService(Context.DEVICE_POLICY_SERVICE); returns null, in API 21.... any Idea? – Saty Jun 01 '15 at 08:41
  • https://code.google.com/p/android-developer-preview/issues/detail?id=1082 somewhat related to this issue @CommonsWare – Saty Jun 03 '15 at 22:10
  • Hi I used your code but I can not remove the application from my phone. I am new and I cant see any code to block removing. Is there any code to disable to remove application from phone? How can I remove it? – ffttyy Sep 07 '15 at 10:22
2
  1. First step is to get your app the DEVICE_ADMIN privilege by the user.

  2. Write your own class which extends DeviceAdminReceiver. For this you need to import import android.app.admin.DeviceAdminReceiver;

  3. Override the methods in your case something like:

    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        Log.d("Hello", "onPasswordFailed");
    }
    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        Log.d("Hello", "onPasswordSucceeded");
    }
    
frogatto
  • 28,539
  • 11
  • 83
  • 129
Saty
  • 1,068
  • 11
  • 24
2

I am doing the same thing on android studio with API level-22. but nothing is happening . Its showing an error- "Installing com.example.sourav.myfirstapp DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.sourav.myfirstapp" pkg: /data/local/tmp/com.example.sourav.myfirstapp Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]"

Here is my detail of my project - Admin receiver main activity-

public class AdminReceiver extends DeviceAdminReceiver {

@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, "u will never break!", Toast.LENGTH_LONG)
            .show();
    String tag="tag";
    Log.v(tag,"this massage from error" );
}

@Override
public void onPasswordSucceeded(Context ctxt, Intent intent) {
    Toast.makeText(ctxt, "good u enterd", Toast.LENGTH_LONG)
            .show();
    String tag="tag";
    Log.v(tag, "this massage from success");
}
}

manifest-

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.sourav.myfirstapp" >
 


 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >

    <receiver
        android:name="AdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data
            android:name="android.app.device_admin"
            />

        <intent-filter>

            <action    android:name="android.app.action.ACTION_PASSWORD_FAILED"/>
            <action     android:name="android.app.action.ACTION_PASSWORD_SUCCEEDED"/>
        </intent-filter>
    </receiver>
</application>

 </manifest>




Metadata-
 
 <device-admin xmlns:android="http://schemas.android.com/apk/res/android">

   <uses-policies>
    <limit-password/>

    <watch-login/>
</uses-policies>
MMG
  • 3,226
  • 5
  • 16
  • 43