Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 570
Shimakaze Avatar asked Jan 18 '12 20:01

Shimakaze


People also ask

How do I change the time on my Memu emulator?

Note that you may need to disable automatic date and time adjustment within the emulator itself, to do that, go into the settings app on the emulator, then "date and time" (near the bottom) and then uncheck the "Use network provided date and time" box.

What is the emulated device for Android?

An Android Virtual Device (AVD) is a configuration that defines the characteristics of an Android phone, tablet, Wear OS, Android TV, or Automotive OS device that you want to simulate in the Android Emulator. The Device Manager is an interface you can launch from Android Studio that helps you create and manage AVDs.


2 Answers

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

like image 192
Artyom Avatar answered Oct 30 '22 23:10

Artyom


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.

like image 29
Peterdk Avatar answered Oct 30 '22 23:10

Peterdk