How to Display Toast on Lock Screen after Failed Password Attempt

Deadly 提交于 2019-12-12 05:14:32

问题


I am trying to show a Toast on the lock screen after a user enters the wrong password 3 times. I am able to verify that the user has failed 3 times through the log console, but would like some message to show on the lock screen so the user knows. I am doing this in a DeviceAdminReceiver. I am able to Toast on a successful password submission, just not a failed one.

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

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

This method allow you to customize your toast : Customizing your 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();



回答2:


The problem is when you show the toast.the lock screen will cover the Toast. because It is not unlocked yet It can be solved by

  1. Send a Notification with a message.
  2. Create a Transparent Activity. with some custom view to display message. and add below flags to your activity. and just start it and set timer to kill itself in 3 second.

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


来源:https://stackoverflow.com/questions/42918111/how-to-display-toast-on-lock-screen-after-failed-password-attempt

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