1

I build an app which fires an activity every time the orientation changes, but I have an exception: I don't want the activity to fire within the next five seconds after phone was unlocked.

So after phone is unlocked I will disregard every phone orientation change for five seconds.
I thought I could get the last-unlock-time and compare it with the current time?

Any suggestions of how to do so?

Thanks!

Trinimon
  • 13,839
  • 9
  • 44
  • 60
SagiLow
  • 5,721
  • 9
  • 60
  • 115

1 Answers1

3

Register a broadcast receiver for ACTION_USER_PRESENT, and start a handler in it to run your activity after a minimum of five seconds.

You'll have to regiester the receiver dynamically, as ACTION_USER_PRESENT is not called if you register from the manifest.

IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
BroadcastReceiver mReceiver = new YourReceiver();
registerReceiver(mReceiver, filter);
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • *ACTION_USER_PRESENT is not called if you register from the manifest.* why only this Action not work from manifest. im not agree on this point because i have already used it many time using manifest – ρяσѕρєя K Mar 28 '13 at 20:36
  • @ρяσѕρєяK Even other actions like ACTION_SCREEN_ON and ACTION_SCREEN_OFF need to be registered dynamically. I read the reason somewhere, but I've forgotten it – Raghav Sood Mar 28 '13 at 20:38
  • 1
    @ρяσѕρєя: I think that is only valid for ACTION_SCREEN_ON/OFF. This article is interesting too: http://funwithdc.wordpress.com/2012/02/12/the-problem-with-androids-action_user_present-intent/ – Trinimon Mar 28 '13 at 20:40
  • @André : i was already used these 3 Intent's by registering in manifest. if u have any official doc( or Romain Guy and CommonsWare blog links ) link about this then share it – ρяσѕρєя K Mar 28 '13 at 20:51
  • @ρяσѕρєя: I would have bet that ACTION_SCREEN_ON doesn't work, if you add it to the manifest (see http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/ too). In my implementation I had definitely to register it dynamically in the code. Anyhow, this was not the question here :) – Trinimon Mar 28 '13 at 20:56
  • @André: i agree,if u have time then plz check [this example](https://sites.google.com/site/imrankhanandroid/Screenunlocktestnew.zip?attredirects=0&d=1) where all three intents working fine from manifest. – ρяσѕρєя K Mar 28 '13 at 21:15