密码尝试失败后如何在锁屏上显示Toast

时间:2017-03-21 03:51:25

标签: android lockscreen android-toast device-admin

我试图在用户输入错误密码3次后在锁定屏幕上显示Toast。我能够通过日志控制台验证用户已经失败了3次,但是想要在锁定屏幕上显示一些消息以便用户知道。我在DeviceAdminReceiver中执行此操作。我能够成功提交密码,而不是失败的密码。

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

public class AdminReceiver extends DeviceAdminReceiver {

    @Override
    public void onPasswordFailed(Context ctxt, Intent intent) {
        Log.d("LockScreen", "onPasswordFailed");
        DevicePolicyManager mgr = (DevicePolicyManager) ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
        int no = mgr.getCurrentFailedPasswordAttempts();
        if (no >= 3) {
            Log.d("LockScreen", "Failed 3 times");
            //Toast does not show
            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();
    }
}

2 个答案:

答案 0 :(得分:0)

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

    public class AdminReceiver extends DeviceAdminReceiver {

        @Override
        public void onPasswordFailed(Context ctxt, Intent intent) {
            Log.d("LockScreen", "onPasswordFailed");
            DevicePolicyManager mgr = (DevicePolicyManager) ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
            int no = mgr.getCurrentFailedPasswordAttempts();
            if (no >= 3) {
                Log.d("LockScreen", "Failed 3 times");
                //Toast does not show
//ctxt is??                
//Toast.makeText(ctxt, R.string.password_failed, Toast.LENGTH_LONG)
//                        .show();
//try this
Toast.makeText(getActivity(), "This is my Toast message!",
   Toast.LENGTH_LONG).show();    
        }
        }

        @Override
        public void onPasswordSucceeded(Context ctxt, Intent intent) {
            Toast.makeText(ctxt, R.string.password_success, Toast.LENGTH_LONG)
                    .show();
        }
    }

此方法允许您自定义吐司: 自定义吐司

LayoutInflater myInflater = LayoutInflater.from(this);
View view = myInflater.inflate(R.layout.your_custom_layout, null);
Toast mytoast = new Toast(this);
mytoast.setView(view);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();

答案 1 :(得分:0)

问题在于你展示吐司。锁定屏幕将覆盖Toast。因为它尚未解锁它可以通过

解决
  1. 发送包含消息的通知。
  2. 创建透明活动。用一些自定义视图来显示消息。并在您的活动中添加以下标记。然后启动它并设置计时器以在3秒内自杀。

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);