4

I have some Intent. When intent fires, I want to send a popup notification like an AlertBox and turn screen ON to let User see the notification immediately (I mean without showing a lockscreen).

If you've used, for example, HandcentSMS then you understand what I mean (like a popup notification when accept a message)

How to organize this? Any code examples? What kind of permissions I have to use?

Thank you in advance.

Ksice
  • 3,277
  • 9
  • 43
  • 67
  • Possible duplicate of [Android: How to turn screen on and off programmatically?](https://stackoverflow.com/questions/9561320/android-how-to-turn-screen-on-and-off-programmatically) – tir38 Jul 19 '19 at 18:12

3 Answers3

7

Check out PowerManager.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
//Do whatever you need right here
wl.release(); 
Kevin Qiu
  • 1,616
  • 1
  • 13
  • 15
  • I've already tried this. My screen waked up but I have lock screen instead of my AlertBox (or my activity.. or just something mine). I don't want lock screen. I want show an Activity! – Ksice Jul 15 '11 at 11:51
  • 5
    i'm pretty sure you have to show a lock because that's kind of the whole point of the lock. what if a user leaves a phone unattended or loses it, and your app turns the screen on without lock? then someone could potentially use saved credit card information or login to the owner's accounts. – Kevin Qiu Jul 15 '11 at 15:18
5

Your activity should have the following code in onCreate() implementation:

@Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

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

      setContentView( R.layout.myLayout);

      // rest of initialization
}

You invoke the activity using an intent. Here is an example:

Intent startIntent = new Intent(context, MyActivity.class);

startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(startIntent);
Calin Grecu
  • 76
  • 1
  • 6
0

Perhaps KeyguardLock will do what you want: popup your notification and then call disableKeyguard, then re-enable when the user is done or after you time out.

Security-wise it is a little risky, but there you go.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • could some one tell me how to achieve this tricky thing ?? I just want to show some alert box as a notification at some particular point of time. ON the LOCK SCREEN. – Cyph3rCod3r Sep 12 '13 at 06:40