Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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

   }    
like image 443
Shan Avatar asked Jul 14 '11 10:07

Shan


1 Answers

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

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

like image 124
Mojo Risin Avatar answered Oct 13 '22 01:10

Mojo Risin