Android: How to check if users date is valid

前端 未结 1 1722
广开言路
广开言路 2021-02-08 17:10

I know there are a lot of questions asked that are related to this but they all seems not to solve my problem. I want to check if the date on the user\'s device is correct, star

相关标签:
1条回答
  • 2021-02-08 18:12

    You must have server's timestamps to determine if the time in device is fake. Npt pool is a free service to help you get true timestamps.
    To use, device must ONLINE, you can not check without the Internet.
    Copy class SntpClient to your project: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/net/SntpClient.java

    Code:

    SntpClient client = new SntpClient();
    long now = -1;
    if (client.requestTime("pool.ntp.org", TIME_OUT)) {              
          now = client.getNtpTime();
          if (Math.abs(now - System.currentTimeMillis())    >= ONE_DAY){ 
               // the device's time is wrong
               startErrorActivity();
               ...
          } else {
               // the different time lower than 1 day
               startNextActivity();
          }
    } else {
          // something wrong, can't get server's time          
    }
    

    Don't forget add INTERNET permission to your manifest

    0 讨论(0)
提交回复
热议问题