Android:如何检查用户日期是否有效
我知道有很多与此相关的问题,但它们似乎都无法解决我的问题。
我想检查用户设备上的日期是否正确,启动一个活动,但是如果用户设备上的日期不正确,它应该显示一个错误活动,要求用户仅调整其日期像whatsapp如何实施他们的应用程序。
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, start an activity but in a case where the date on the users device is wrong, it should show an error activity that asks the user to adjust their date just like how whatsapp implemented theirs..
您必须具有服务器的时间戳,才能确定设备中的时间是否为假。
Npt池是一项免费服务,可帮助您获得真实的时间戳。
要使用,设备必须在线,您必须在没有Internet的情况下进行检查。
将类 SntpClient 复制到您的项目中: https://android.googlesource.com/platform/frameworks/base/+ /master/core/java/android/net/SntpClient.java
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
代码:
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
}
别忘了在清单中添加 INTERNET
权限
Don't forget add INTERNET
permission to your manifest