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



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

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

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 时。锁定屏幕将覆盖 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);
    

相关内容

  • 没有找到相关文章

最新更新