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

喜夏-厌秋 提交于 2019-12-04 08:18:06

问题


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?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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)




回答5:


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


来源:https://stackoverflow.com/questions/8916609/emulated-android-device-does-not-re-sync-time-date-after-restoring-snapshot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!