如何获取模式或密码写错的消息

时间:2015-08-29 20:10:12

标签: java android

我想要一个计算甚至得到密码或手机模式错误的功能?

public int number_of_times ()

} return times; }

1 个答案:

答案 0 :(得分:1)

仅当您使用设备管理API时才可以这样做。具体来说,您需要:

  • DeviceAdminReceiver子类,在清单中正确注册,您可以在其中找到有关密码失败的信息

  • 设备管理员元数据,表示您希望了解设备管理员失败

  • 帮助用户进入“设置”应用并以设备管理员身份启用应用的用户界面

https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener演示了所有这些,作为稍微大一点的样本的一部分,它也设置了密码质量要求。

值得注意的是,清单中的AdminReceiver元素通过在ACTION_PASSWORD_FAILED中加入<intent-filter>操作来通知密码失败:

            

        <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>

此外,我的设备管理元数据(res/xml/device_admin.xml)包含<watch-login/>政策:

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

    <uses-policies>
        <limit-password/>

        <watch-login/>
    </uses-policies>

</device-admin>

此外,我的AdminReceiverDeviceAdminReceiver的子类并覆盖onPasswordFailed(),以找出有关失败的信息:

/***
  Copyright (c) 2013 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
 */

package com.commonsware.android.pwenforce;

import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

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();
  }
}