Some users of my Android application report bugs when the mobile enters sleep/power saving mode.
I would like to test that issue on the Android Virtual Device. Is it possible to simulate the mobile entering in sleep/power saving mode on the AVD ?
Thanks in advance.
Try the following on emulator
Go to Settings
Go to Developer options
Un-check Stay awake (3rd option from the top)
The emulator will go to sleep after 1 min (default setting) if not changed.
Press the power button to wake up the emulator.
Developer options is not enabled by default, to enable it, do the following:
Go to Settings
Click About emulated device
Click Build number 7 times, you should be notified that Developer options is now enabled
Go back and you should see Developer options
On emulator, go Settings/Security/Screen lock change from 'None' to 'Swipe'
Then, click 'power' button on right control bar to turn off screen, and press again to turn on screen. Now the lock screen will show up.
The Power Button on the sidebar next to emulator will do it.
The hotkey on my Mac is ⌘ P
Note: You will need to set Lock Screen using Swipe instead of None
To put the device to sleep using command line, run:
adb shell input keyevent 223
To wake the device from sleep using command line, run:
adb shell input keyevent 224
For more info about key events you can send with ADB, check out the KEYCODE_... constants for KeyEvent, e.g.:
/** Key code constant: Sleep key.
* Puts the device to sleep. Behaves somewhat like {#link #KEYCODE_POWER} but it
* has no effect if the device is already asleep. */
public static final int KEYCODE_SLEEP = 223;
/** Key code constant: Wakeup key.
* Wakes up the device. Behaves somewhat like {#link #KEYCODE_POWER} but it
* has no effect if the device is already awake. */
public static final int KEYCODE_WAKEUP = 224;
Somehow fn + F7 doesn't work on my mac. So what I use instead is:
adb shell input keyevent 26
This sends the POWER KEY event and will turn off the screen. Note: It will not show that the screen is off. Image wills tay. But you can't interact with it. As soon as you do adb shell input keyevent 26 again, you will see the lock screen indicating, that the device was off before.
By pressing F7 you can emulate sleep mode in your emulator.
Related
I am trying to set an automatic on/off timer for a device that is running Android 9 (For examples sake let's say I want it to turn on at 9 AM and turn off at 5PM) This must happen daily.
I want this to be done using adb commands so that it can be automated down the line.
If I go into settings I can navigate as so: Settings > Accessibility > Scheduled Power on and off
once in here I can set the Power off time, Power on time and the 2 relative Repeats
Any changes I make (physically or via adb) are then required to be confirmed by pressing the Save Settings button at the bottom of the screen.
Pressing the Save Settings button triggers the introduction of multiple variables:
close_machine_time_hour
close_machine_time_mins
open_machine_time_hour
open_machine_time_mins
power_off_cycle_mode
power_on_cycle_mode
power_off_date
power_on_date
power_way
machine_time_secs
Of which the 2 I am most concerned about are the last 2 as these seem like odd variables to only introduce when activating a niche setting (I feel as though they should either be there always)
Question 1: would be is this a standard Android feature? Or is this something that has been done for custom ROM?
I have tried setting all the settings as they appear in settings grabbed using command adb shell settings list system
They are set individually using the commands below:
adb shell settings put system timer_power_switch_settings 1
adb shell settings put system close_machine_time_hour 09
adb shell settings put system close_machine_time_mins 00
adb shell settings put system open_machine_time_hour 17
adb shell settings put system open_machine_time_mins 00
adb shell settings put system power_off_time 9:00:16
adb shell settings put system power_on_time 17:00:16
adb shell settings put system power_off_cycle_mode 0-1-2-3-4-5-6-
adb shell settings put system power_on_cycle_mode 0-1-2-3-4-5-6-
You'll note that in essence I am setting the time twice, which is quite odd.
The key thing is that none of this works unless I physically press the save button (despite showing visual feedback on the device screen that values change as I alter them via adb). Which is fine for 1 device but will become quite laborious if it needs to be done for 30+ devices at a time.
Question 2: Is there to simulate this Save Settings button click without simulating a tap on screen? Or to bypass this setting entirely?
Question 2.A: Is there a way to continuously monitor activity using an adb command? so I can run it, then click the button, and it will show me what's going on in the background?
Working on android recovery mode. pressing volume up & power button will open Android recovery mode . the hardware i am using , that does not have any volume button. I want to define new key options to enter into recovery mode. how can i change the options from Volumeup+power to some other option ?
You cannot do that. That piece of information is handled by the hardware manufacturers and you won't have access to it. You can instead use adb to go to recovery mode by using the following command.
adb reboot recovery
I need to access to programmatically turn ON & OFF Power Saving Mode in android.
Is there any API for this ? As I can see Power Saving mode option is not available
for all mobile devices, is there any code to check these settings availability and if available then turn it ON and OFF ?
Also is there any work around ?
If you want to test the Power Saving Mode (PSM):
The following works with Android Studio 3.4.1 and a Google Pixel 2 XL phone:
$ adb shell dumpsys battery unplug # a charging device cannot enter PSM
$ adb shell settings put global low_power 1 # enter Power Saving Mode (PSM)
This only works on devices that has the standard Power Saving Mode, like Samsung's, but not Huawei's, for example. On some Samsung devices, this setting is stored in global setting keyed psm_switch, as mentioned above.
You can leave PSM by setting low_power to 0 or by enabling charging again with:
$ adb shell dumpsys battery reset
Source: https://developer.android.com/training/monitoring-device-state/doze-standby#testing_doze_and_app_standby
It's bad practice to control the user's device settings from your app.
Rather consider suspending background services when battery levels are low.
Or to notify the user of low battery power and advising the user to switch of unnecessary settings like Bluetooth or WiFi.
Most devices won't have a built-in power-saver API, so you could try to make your own power-saver mode by turning off the bluetooth if it's on, the wi-fi, etc, deactivating location in settings, or checking for other power-eating options enabled.
There are no available API's to do this in android SDK. however You can control/save the power by Turning off Blutooth,Wi-Fi and other unused sevices inside your coding.
You can't set the power saving mode programmatically. Allowing this would be a bad idea.
On certain Samsung devices you can check if it is enabled:
final String result = Settings.System.getString(getContentResolver(), "psm_switch");
Log.v("Debug", "Powersaving active: " + TextUtils.equals(result, "1"));
See this for more info: https://stackoverflow.com/a/39296959/3600178
When build and run iOS app using Xcode, the phone becomes awake and app runs. Is there a way to wake and unlock Android phone (or tablet) screen then run installed Android app (Eclipse, Android Studio)?
One solution: set the following flags in your activity class(es):
if (BuildConfig.DEBUG) {
// These flags cause the device screen to turn on (and bypass screen guard if possible) when launching.
// This makes it easy for developers to test the app launch without needing to turn on the device
// each time and without needing to enable the "Stay awake" option.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
These flags will:
Turn on the screen
Bypass the lock-screen
Allow showing the activity even if the device is locked
By setting those flags in your base activity, you'll be able to continue using your application even if the device was off and/or locked at the time of running. If you try to leave your application's process (i.e., hitting the home button, or switching to another app), the lock screen will appear, and you'll have to manually unlock to continue using the device.
Warning: this should only be used while developing/debugging your application, so I recommend keeping the if (BuildConfig.DEBUG) check, as it is in this example
Better and easiest solution is use the option (in development section) that the screen never goes off I thing the name is "Stay awake". This option prevent your phone to get locked when the usb cable is connected.
Another way is to write a script which runs before the app deploys and unlocks the device. This requires no app code change and will not change screen timeout which can have security implications.
You can find the full setup here
#!/bin/bash
# When a device is attached there will be atleast 3 lines -> heading, device details, an empty new line
if adb devices | wc -l | grep "3"; then
# Check if device locked, this may differ on some OEMs
if adb shell dumpsys window | grep "mInputRestricted=true"; then
echo "Device is Locked"
adb shell input keyevent KEYCODE_WAKEUP # wakeup device
adb shell input touchscreen swipe 530 1420 530 1120 # swipe up gesture
adb shell input text "000000" # <- Change to the your device PIN/Password
#adb shell input keyevent 66 # simulate press enter, if your keyguard requires it
else
echo "Device already unLocked"
fi
# 2 = Stay awake on USB, 0 = reset
adb shell settings put global stay_on_while_plugged_in 2
#adb shell settings put system screen_brightness 700
adb shell input keyevent KEYCODE_WAKEUP
adb shell input touchscreen tap 0 0 # this will wake up the screen and won't have any unwanted touches
else
echo "There should be only one device connected at a time"
fi
return 0
You cant unlock and wake the phone as that would not be very secure, however you can enable "Stay awake" in developer options.
How can I turn the sceen on ?
I tried something like this
adb -d shell am broadcast -a android.intent.action.SCREEN_ON
It really should work, I send broadcast intent it is received by the system, but the screen doesn't turn on
I do not understand what is the problem, is it possible to turn the screen of the device by code ? I mean with software ? Cause it seems like the turning on of the screen is done just by the hardware button press . . . at least I got that felling , am I wrong ?
adb shell input keyevent KEYCODE_POWER
Works to turn on screen (when display is off)
Works to turn off screen (when display is on/awake)
For Android 5.0 and above:
adb shell input keyevent KEYCODE_WAKEUP
or
adb shell input keyevent 224
KEYCODE_WAKEUP
Key code constant: Wakeup key. Wakes up the device. Behaves somewhat like KEYCODE_POWER but it has no effect if the device is already awake.
https://developer.android.com/reference/android/view/KeyEvent#KEYCODE_WAKEUP
Note: KEYCODE_POWER has been added in API level 1, while KEYCODE_WAKEUP has been added in API level 20!
u can turn it on/off if u do like:
adb shell
#shell: input keyevent 26
#shell: (enter or via hidden command empty line)
#shell: exit
this worked for me on some android versions ;)(NOTE: this will turn the screen on and off, depends on the actual screen state)
To detect the current state of the screen u can use the following ways:Android < 5.x.xadb shell dumpsys input_methodIn the output search for mScreenOn=true/false
Android >= 5.x.xadb shell dumpsys displayIn the output search for mScreenState=ON/OFF
In my scripts i use this \s{0,}mScreen(State|On)=(?<STATE>(true|false|on|off))\s{0,} (Compiled|IgnoreCase|ExplicitCapture) regular expression for both outputs to detect the current state.
EDIT (16.03.2018):
There is also another way to detect the screen state, it works since Android 3.0. The dumpsys window policy command will give us all we need. - In the output search for mScreenOn(Fully)?=(?<STATE>(true|false)).
There are also other useful informations like:
mUnrestrictedScreen (value is like: (0,0) 768x1280)
mRestrictedScreen (value is like: (0,0) 768x1184)
Regards,
k1ll3r8e
I could be wrong about this, but...
You shouldn't think of broadcasts as something to send to get things done, but instead think of them as things that are sent when things are done.
I think the system sends 'android.intent.action.SCREEN_ON' when screen is goes on, but sending 'android.intent.action.SCREEN_ON' does not necessarily make the screen go on.
I hope this makes sense.
For the answer, you can find it in...
Calling hidden API in android to turn screen off
turn the screen on/off in Android with a shake
The command to toggle the screen on/off is:
adb shell input keyevent 26
This condensed command is preferred because it allows you to use it in scripts.
Cheers!
this works in android 12
#!/bin/bash
screenState=$(adb shell dumpsys window policy | grep screenState=SCREEN_STATE_ | cut -c 32-)
if [ "$screenState" == "OFF" ]; then
adb shell input keyevent KEYCODE_POWER
fi