【问题标题】:How to Display Toast on Lock Screen after Failed Password Attempt密码尝试失败后如何在锁定屏幕上显示 Toast
【发布时间】:2017-08-12 14:06:14
【问题描述】:

在用户输入错误密码 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();
    }
}

【问题讨论】:

    标签: android lockscreen android-toast device-admin


    【解决方案1】:
        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();
    

    【讨论】:

    • 允许自定义 toast 的方法,会在 DeviceAdminReceiver 中吗?
    【解决方案2】:

    问题是当您显示 toast 时。锁定屏幕会覆盖 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);
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 2021-04-24
      • 2020-05-07
      相关资源
      最近更新 更多