6

How to find whether phone is in sleep/idle mode for Android?

My problem is I am able to wake up the phone from sleepmode by using alarm manager

But when the phone is not sleep mode and at the same instant if Alarm manager is used to wake the phone..android force closes the App..

whether there is anyway to find whether the Phone is in sleep or idle mode?(Black screen)

Update:

My requirement:

When the phone is in sleep mode ---> Intent should be launched from the service When the phone is not in sleep mode --> The same Intent should be launched from the service

None of the solutions below worked perfectly so here is my little tweak which worked perfectly :):)

  //Pending intent for my alarm manager 
  Intent intentaa=new Intent(this,MyBroadcastReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentaa, 0);

  //Intent to be launched where MyIntent is the intent
  Intent intenta = new Intent();
  intenta.setClass(this, MyIntent.class);
  intenta.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  //Real code
  try
  {
   startActivity(intenta); 

  }catch(Exception E)
  {

      AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
      am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(),
      pendingIntent);
      startActivity(intenta); 

   }    
Shan
  • 2,822
  • 9
  • 40
  • 61

4 Answers4

15

You can check if the screen is turned off by calling isScreenOn method.

Note:This only can be used for 2.1 and above..

Mojo Risin
  • 8,136
  • 5
  • 45
  • 58
  • 1
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); boolean isScreenOn = ((Object) pm).isScreenOn(); I get a error message or isScreenOn methofd for adding a cast – Shan Jul 14 '11 at 15:27
  • When I use the isScreenon code given over there..I am getting this error "Add cast to pm" and when I add cast I am again getting the same error – Shan Jul 14 '11 at 15:59
  • Which version of the sdk are you using. It defined for api level >= level 7 – Mojo Risin Jul 14 '11 at 17:38
  • So in this case you'll have to use MByD's solution – Mojo Risin Jul 14 '11 at 22:47
  • `boolean isScreenOn = ((Object) pm).isScreenOn();` what on earth are you trying to do ? – xmen May 17 '13 at 05:58
  • `isScreenOn` deprecated at API 20, use `isInteractive()` instead. – AlexBcn Jul 03 '14 at 11:27
5

Check out isInteractive()

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
  return pm.isInteractive();
} else {
  return pm.isScreenOn();
}
Noel Chew
  • 3,873
  • 2
  • 15
  • 16
5

isScreenOn method returns only if the screen is on or off. It does not indicate the sleepy mode, because Android sleep mode is not same as Android screen off mode. In Android, sleep mode might come within milliseconds (depending on wakelocks) after screen off (idle mode) - but that's not guaranteed (referring to this post).

I think you can try using SystemClock.uptimeMillis(),SystemClock.elapsedRealtime() comparison.

Community
  • 1
  • 1
0

I'm not sure thats what you want, but you can use Intent.ACTION_SCREEN_OFF to determine. see here an example.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • Your solution works fine except when the app is installed after ACTION_SCREEN_OFF is sent - e.g. when the app is installed with screen turned off. – Mojo Risin Jul 14 '11 at 11:18