How to find if the phone is in standby / standby mode for Android

How to find if the phone is in standby / standby mode for Android?

My problem is that I can wake the phone from sleep mode using the alarm manager

But when the phone is not in sleep mode and at the same moment, if the alarm manager is used to swell the phone. Android power shuts down the app.

is there any way to find if the phone is in standby or standby? (black screen)

Update:

My requirement:

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

None of the solutions below work fine, so here is my little tweak that worked fine :) :)

//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); } 
+6
source share
4 answers

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

Note. This can only be used for version 2.1 and higher.

+15
source

isScreenOn method is returned only if the screen is on or off. It does not indicate sleepy mode, since the Android sleep mode is not the same as the Android screen off mode. In Android, sleep mode can enter in milliseconds (depending on wakelocks) after turning off the screen (standby mode), but this is not guaranteed (referring to this post ).

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

+5
source

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(); } 
+2
source

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

0
source

Source: https://habr.com/ru/post/892739/


All Articles