How To Display Toast On Lock Screen After Failed Password Attempt
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 wou
Solution 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;
publicclassAdminReceiverextendsDeviceAdminReceiver {
@OverridepublicvoidonPasswordFailed(Context ctxt, Intent intent) {
Log.d("LockScreen", "onPasswordFailed");
DevicePolicyManagermgr= (DevicePolicyManager) ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
intno= 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();
}
}
@OverridepublicvoidonPasswordSucceeded(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
LayoutInflatermyInflater= LayoutInflater.from(this);
Viewview= myInflater.inflate(R.layout.your_custom_layout, null);
Toastmytoast=newToast(this);
mytoast.setView(view);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();
Solution 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
- Send a Notification with a message.
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);
Post a Comment for "How To Display Toast On Lock Screen After Failed Password Attempt"