Change android time zone on emulator via adb shell - android

I'm running tests for my app, and I want to validate that it works in various time zones.
I'm trying solutions suggested here running adb shell setprop persist.sys.timezone "Pacific/Honolulu", but the timezone (and time) on emulator is not changing- can this work without restarting the device?

If you mean UI tests, I can not change android Date & Time settings on emulator via adb shell.
The only solution I found is to set the timezone during the creation of the emulator with -timezone option.
For example, you can create android emulator with a command like $ emulator #Nexus_5X_API_23 -timezone Europe/Paris.
See more emulator command line options here

Related

See app background processes in Android Studio

Maybe I'm just missing it but is there a way to view WHAT is running in the background on-device in android studio?
I'm getting a battery usage alert on my phone (galaxy s8 - OS v8.0), indicating my app is doing something in the background and I'd like to see what it is.
Thank you
Yea, via the terminal function. This is one of options at the bottom of the Android studio.
You need to run ADB to connect to the device. Once connected you can use Unix command lines to see the processes running.
From google dev : https://developer.android.com/studio/command-line/adb
Also, the command I use to connect an emulator to run unix commands is:
adb -s <DEVICE> shell
Normally the ADB is stored :
USER\AppData\Local\Android\Sdk\platform-tools
and then once connected you can use the following to see the running processes :
https://www.howtogeek.com/107217/how-to-manage-processes-from-the-linux-terminal-10-commands-you-need-to-know/
Hope this helps.

create 3 android emulators [phone, tab7, tab10] and start them via commandline for fastlane

I'm working on fastlane and want to start 3 emulators [phone, tab7, tab10] and take screenshots on them.
How do I create a script to create and start emulators via fastlane or commandline
You will need a script which does the following:
Start the emulator
Wait for the emulator to boot
Start screengrab for this emulator
Shut down the emulator
Then you call the script for your three different screen sizes.
Some more details:
To start the emulator use the program emulator which is located under android-sdk/emulator/emulator. I had problems with android-sdk/tools/emulator so be sure to use the right one. The call can look like this:
emulator #'your_emulator_name_variable' &
The & is important when you are using a shell script to continue after the call.
You can call adb shell getprop sys.boot_completed to see if the emulator has booted already. Do this in a loop until it returns 1
Call fastlane screengrab and append the specific type flag:
--specific-type 'device_type_variable'
with 'device_type_variable' equals to phone, sevenInch or tenInch. According to the emulator you started.
Call adb emulator-5554 emu kill to shut down you emulator. If it uses another, non standard port, adjust accordingly.
Now you can call this script with the two variables your_emulator_name_variable and device_type_variable for each of your emulator.
Hope this helps.

Emulated Android device does not re-sync time/date after restoring snapshot

If I do a fresh boot on the emulated device, it gets the correct current time from the host OS; however, if I reload the device from a snapshot, it gets the time/date from the moment the snapshot was created (e.g. When I shut down the emulator). The time/date does not re-sync after any amount of time. The only way around it that I've found is to manually update the time after restoring from a snapshot.
The Android Virtual Device has default properties:
Target = Android 4.0.3 - API Level 15
CPU/ABI = ARM (armeabi-v7a)
SD Card = N/A
Snapshot = Enabled
Abstract LCD density = 240
Max VM application heap size = 48
Device RAM size = 512
I've tried the emulator on OS X Snow Leopard and Windows 7, both show the same problem. Is there any way to get the emulator to automatically sync time after restoring from snapshot?
I have been running into the same problem, and there does not seem to be a standard way of doing this. However, an emulator's date and time can be updated using the date command of the ADB shell, which can be used in conjunction with standard commands for displaying date and time on your OS to update the emulator date and time to the current date and time.
To set a date and time of the emulator, you need to execute the following command in your OS:
adb shell date -s YYYYmmdd.HHMMSS
where YYYYmmdd is the date and HHMMSS is the time.
Linux
Setting the emulator date and time to the current date and time is relatively straightforward from a UNIX-style shell, so the following command will work on Linux:
adb shell date -s `date +"%Y%m%d.%H%M%S"`
macOS
adb -e shell su root date `date +"%m%d%H%M%y"`
Windows
On Windows (which I am using), the easiest way to do it is through Windows PowerShell:
adb shell date -s $(get-date -format yyyyMMdd.HHmmss)
In Command Prompt, it is a bit more tricky because there is no way to specify a custom format to display date and time. The best way I found to get it in locale-independent format is by using the command wmic os get LocalDateTime (line 2). Its date-time format can be parsed to adapt to the format needed by the ADB shell: the symbols :~ can be used to print a substring of an environment variable contents, with the format %var:~<start-index>,<number-of-chars>%. We also need to ignore everything except line 2, so the full command that you need to run is as follows:
for /f "skip=1 delims=" %A in ('wmic os get localDateTime') do #for /f "delims=" %B in ("%A") do #cmd /v /c "set wmicdate=%B & adb shell date -s !wmicdate:~0,8!.!wmicdate:~8,6!"
For the curious: this first saves the date-time into the %wmicdate% variable and then passes it to ADB by parsing it appropriately. The ! are used instead of % to read the variable on-the-fly. This is all done in a child cmd process launched with the /v option that enables this on-the-fly variable reading.
EDIT: Fixed the command for macOS (thanks #user836003).
I opened a bug report.
I have the same kind of issues, and found out the hard way because my app that uses SSL, kept giving very weird errors. This was due to wrong date and time.
Apparently it's not yet reported.
On a newer Android emulator running version 6 API 23, the following powershell command worked for me.
Windows Powershell
adb shell date $(get-date -format MMddHHmmyyyy.ss)
On Android emulator version 7 API 24:
adb shell su root date $(get-date -format MMddHHmmyyyy.ss)
I have searched many times before for a solution to this and i searched again when i saw your question but i couldn't find anyone else even complaining about this except you and me, maybe others don't create apps that time is critical or they test on a real device.
Conclusion: no there is not fix, you have to set it manually or not use snapshots.
Voted Arthon's answer up.
It seems that the emulator get loose to sync when the host machine get sleep.
I'm, personally, using following program for this.
public class AdbShellDateNow {
public static void main(final String[] args)
throws java.io.IOException, InterruptedException {
final long now = System.currentTimeMillis() / 1000L;
final ProcessBuilder builder =
new ProcessBuilder("adb", "shell", "date", Long.toString(now));
builder.redirectErrorStream(true);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
final Process process = builder.start();
process.waitFor();
}
}
This one from macOS works best for me since it takes the time of the host to sync:
adb shell su root date -u #$(date -u +%s)

Android: how to change the time in emulator?

I've noticed that the time in my emulator for android projects is wrong. Its one hour behind.
How do I go about changing the time and can I do it in eclipse?
Run>Debug Configurations/Run Configurations
Tab Target> Additional Emulator Command Line Options
-timezone America/New_York
use this command
C:\> adb shell
#date –- 2009-10-01 14:24:59
20070325.123456
#date –s 20070325.123456
I believe the emulator is set to the GMT timezone by default. You can specify the timezone for the emulator with the -timezone parameter.
https://developer.android.com/studio/run/emulator-commandline.html
If you want to change date & time, from running emulator window go to:
Apps -> Settings -> Date & Time -> Disable Automatic date & time -> Set date & Set time
If you want to change timezone, from running emulator window go to:
Apps -> Settings -> Date & Time -> Disable Automatic time zone -> Select time zone
If you use IntelliJ you can do that from Run/Edit Configurations window.
Go to Emulator tab and add this to "Additional command line options":
-timezone Europe/Helsinki
Android document gives this info:
-timezone Set the timezone for the emulated device to , instead of the host's timezone. must be specified in zoneinfo format. For example: "America/Los_Angeles"
"Europe/Paris"
Zoneinfo format is also known as tz database. So to find you specific timezone, you can use the Wikipedia list here:
http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
Increase emulator date by some certain hours, Perfectly working
Here i am increasing time by 4 hours.
adb root
adb shell "date date +09190400"
Month & Date : 0919(mmdd)
hours : 04
I have searched extensively, and it seems like the only option is to turn off the Auto-Set time in the emulator phone prefs, and manually set the timezone. I can't find any place to set emulator options from inside Eclipse/AVD Manager.
Set the time on the emulator via ADB:
Bash:
adb root
adb shell "date $(date +%m%d%H%M%G.%S)"
adb shell "am broadcast -a android.intent.action.TIME_SET"
Powershell:
$currentDate = Get-Date -Format "MMddHHmmyyyy.ss" # Android's preferred format
adb root
adb shell "date $currentDate"
adb shell "am broadcast -a android.intent.action.TIME_SET"
The am broadcast signal the clock to update in the status-bar, though its not needed according to my tests on emulator API-31.
The adb root lets following adb command execute with root-access, which is needed to set the date.
Thanks to RipTutorial
See also duplicate Stackoverflow with more info for special android-devices: Set date/time using ADB shell
refer to this page
http://ysl-paradise.blogspot.com/2008/09/android.html
in the comment part.
The question was 9 years ago, but these days (on a mac at least) you just change your mac's time in the OSX System Preferences Date & Time pane and it's immediately mirrored in the Android emulator.

Changing the Android emulator locale automatically

For automated testing (using Hudson) I have a script that generates a bunch of emulators for many combinations of Android OS version, screen resolution, screen density and language.
This works fine, except for the language part.
I need to find a way to change the Android system locale automatically. Here's some approaches I can think of, in order of preference:
Extracting/editing/repacking a QEMU image directly before starting the emulator
Running some sort of system-locale-changing APK on the emulator after startup
Changing the locale settings on the emulator filesystem after startup
Changing the locale settings in some SQLite DB on the emulator after startup
Running a key sequence (via the emulator's telnet interface) that would open the settings app and change the locale
Manually starting the emulator for each platform version, changing the locale by hand in the settings, saving it and archiving the images for later deployment
Any ideas whether this can be done, either via the above methods or otherwise?
Do you know where locale settings are persisted to/read from by the system?
Solution:
Thanks to dtmilano's info about the relevant properties, and some further investigation on my part, I came up with a solution even better and simpler than all the ideas above!
I have updated his answer below with the details.
Personally I think the simplest way is to start the emulator, probably a clean instance unless you are running integration tests that depends on other applications and then change locale using adb:
$ adb shell '
setprop persist.sys.language en;
setprop persist.sys.country GB;
stop;
sleep 5;
start'
or whatever locale you want to set.
To verify that your change was successful just use
$ adb shell 'getprop persist.sys.language'
You may also want to run emulators on know ports, check my answer in this thread.
Note that you can also set system properties directly when starting the emulator:
emulator -avd my_avd -prop persist.sys.language=en -prop persist.sys.country=GB
This way, you can create a plain old emulator of any type then start it up immediately using the locale of your choice, without first having to make any modifications to the emulator images.
This locale will persist for future runs of the emulator, though of course you can always change it again at startup or during runtime.
Accepted answer doesn't work anymore. persist.sys.language and persist.sys.country are gone from emulator properties.
My solution is to use preinstalled on android emulator "Custom locale" application. Simply send intent with extra language parameter to it as below:
adb shell am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE EN
More information here - prepare android emulator for UI test automation.
UPDATE: based on comment from Jonas Alves the following command works on API 28+:
adb shell am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE "en_US" com.android.customlocale2
Seems that Android emulator now supports setting the locale when starting it:
emulator -avd <avd-name> -change-locale fr-CA
Source - https://androidstudio.googleblog.com/2019/09/emulator-29112-stable.html

Categories

Resources