Android: Turn screen ON and notify user
I have some Intent. When intent fires, I want to se开发者_如何学Cnd 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.
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();
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);
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.
精彩评论