0

I'm trying to display a Toast notification in verifyPasswordAndUnlock() in PasswordUnlockScreen.java whenever a user enters the wrong password. I wrote the following code, and built successfully.

 Toast.makeText(getContext(), "Invalid password", Toast.LENGTH_LONG).show();

However, the Toast doesn't appear, because it actually appears in the home screen, which is invisible when the lock screen is on top. I verified this by having another Toast appear when the user successfully logs in, and that Toast only appears in the home screen after the Lock screen disappears

I suspect this is because getContext() returns the home screen's context. How do I get the Lockscreen's context? PasswordUnlockScreen.this doesn't even compile, and mContext and context both have the same problem as getContext()

user1118764
  • 9,255
  • 18
  • 61
  • 113
  • maybe using getContext.getApplicationContext() ? – njzk2 Sep 18 '12 at 08:20
  • getContext.getApplicationContext() doesn't work either, it causes a boot loop once password lock is set, due to fatal exception in system process(java.lang.NullPointerException), probably coz PasswordUnlockScreen isn't at the application level. – user1118764 Sep 18 '12 at 08:49
  • Code is too long to paste. Basically I put put the Toast in verifyPasswordAndUnlock() function when it fails the verification. – user1118764 Sep 18 '12 at 08:50
  • this is your response [100% tested][1] [1]: http://stackoverflow.com/questions/11257691/how-to-show-a-message-when-the-phone-is-unlocked – Mustafa Kamali Aug 20 '15 at 18:43

2 Answers2

0

All views do have mContext field which is initialized to context.

Toast.makeText(mContext, "Invalid password", Toast.LENGTH_LONG).show();
nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • Unfortunately it seems that PasswordUnlockScreen doesn't have mContext. You can look at the code at https://www.codeaurora.org/git/projects/qrd-gb-ssss-7225/repository/revisions/ceb88d1af25c5412027e8384f056f502900396ef/entry/android/frameworks/policies/base/phone/com/android/internal/policy/impl/PasswordUnlockScreen.java What's weird is that if I use mContext, it builds, but I get the same problem. Should I add code to initialize mContext to context in the constructor? – user1118764 Sep 18 '12 at 08:31
  • All Views have mContext and PasswordUnlockScreen is a View. so the problem is not because you are using wrong context, I guess the Toast will never be displayed on top of LockScreen, You will have to create your own dialog box which is of type SYSTEM_ALERT_WINDOW – nandeesh Sep 18 '12 at 08:35
0

As has been said already, the problem is probably not with the Context. I guess you are working with the actual PasswordUnlockScreen source, and not subclassing it? If so, you could try using its KeyguardStatusViewManager to display your text:

mStatusViewManager.setInstructionText("Invalid password");
ekholm
  • 2,543
  • 18
  • 18
  • Thanks. I'm working on Gingerbread source, and it seems I don't have a KeyguardStatusViewManager, so that line of code doesn't work. Is there any other way of displaying the message in this case? – user1118764 Sep 18 '12 at 08:58
  • I had a look at the Gingerbread source, and it seems they are only using the title text view to show text (`mTitle.setText(...)`). Actually it looks like `KeyguardStatusViewManager` (in 4.0.1 and later) also only modifies an existing TextView, the only difference is that it is reset to its original value after a timeout. – ekholm Sep 18 '12 at 11:17
  • Yup that seems to be the case. I guess the only way would be to add another TextView in the layout. Trying that now... – user1118764 Sep 19 '12 at 01:41