Detect Lock Screen Incorrect Password by user in Android

流过昼夜 提交于 2019-11-28 20:56:16

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:

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>

Saty
  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");
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!