我知道有很多问题与此有关,但它们似乎都没有解决我的问题.我想检查用户设备上的日期是否正确,启动活动但是在用户设备上的日期错误的情况下,它应该显示一个错误活动,要求用户调整他们的日期,就像whatsapp实现的方式一样他们..
您必须具有服务器的时间戳,以确定设备中的时间是否为假.
Npt pool是一项免费服务,可帮助您获得真正的时间戳.
要使用,设备必须在线,没有互联网就无法检查.
将类SntpClient复制到您的项目:https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/net/SntpClient.java
码:
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
}
Run Code Online (Sandbox Code Playgroud)
不要忘记INTERNET
为清单添加权限