how to show error if on device time does not match with internet time in android studio like WhatsApp

huangapple go评论69阅读模式
英文:

how to show error if on device time does not match with internet time in android studio like WhatsApp

问题

我正在制作一个安卓应用,该应用显示相对于设备时间的上传时间,但如果设备的时间和日期设置不正确,我无法获得所期望的结果。因此,如何在设备的时间和日期不正确或与互联网时间不匹配时显示警告或错误。除非用户正确设置日期和时间,否则该应用不应该运行。

英文:

im making an android app which shows time of upload relative to the device time, but if the device time and date is not set correct then I do not get the desired result. so how to show warning or error if the device time and date is not correct or matches to the internet time. the app should not work unless the user set the date and time correct.

答案1

得分: 0

从上传的API获取一个额外的UTC DateTimeOffset参数作为响应,然后将其转换为您的本地时区。

缺点 -

> 如果设备中的时间设置不正确,则必须创建一个本地化方法,该方法可以将时间戳转换为适当的时间,而不干扰本地时区。

您可以使用静态的时区标识进行转换,或者通过内部API调用获取时区标识。我使用Xamarin,所以对我来说是这样的 -

var timeZoneId = "Asia/Calcutta";  // 在全球应用程序中使用 TimeZoneInfo.Local.ToString();
DateTime localizedDateTime = TimeZoneInfo.ConvertTimeFromUtc(incomingDATE.ToUniversalTime(), TimeZoneInfo.FindSystemTimeZoneById(timeZoneId));

因此,将收到的UTC DateTimeOffset转换为本地时间。

英文:

Get an extra UTC DateTimeOffset parameter as a response from the uploaded API & then convert it into your local timezone.

Cons -

> If time is set wrong in the device, you have to create a localization method for it which can convert the timestamp to proper time without interfering with the local timezone.

You can use static timezoneid for conversion or get it by internal API calls, I uses Xamarin soo for me it's like -

var timeZoneId = "Asia/Calcutta";  // use it for worldwide application usage TimeZoneInfo.Local.ToString();
DateTime localizedDateTime = TimeZoneInfo.ConvertTimeFromUtc(incomingDATE.ToUniversalTime(), TimeZoneInfo.FindSystemTimeZoneById(timeZoneId));

Soo convert the received UTC DateTimeOffset to Localized time.

答案2

得分: 0

这个东西在我的应用程序中效果最好。我使用 jsoup 来搜索谷歌时间,获取当前时间,然后将手机时间与谷歌时间进行比较。因此,如果这些时间不同,您可以通过对话框或警告框停止用户使用,告诉他们时间已经改变。您可以在 MainActivity 中实现以检查此条件。
以下是代码片段,以便您更清楚地了解:

public class HomeActivity extends AppCompatActivity {
    //phoneDate 和 phoneTime 用于获取当前手机日期和时间
    String phoneDate = new SimpleDateFormat("dd MMM yyyy ").format(clnd.getTime()).trim();
    String phoneTime = new SimpleDateFormat("hh:mm a").format(clnd.getTime()).trim();
    String googleDate;
    String googleTime ;

    @Override
    protected void onCreate(Bundle _savedInstanceState) {
        super.onCreate(_savedInstanceState);
        setContentView(R.layout.home);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try  {
                    //URL 用于搜索时间
                    String url = "https://www.google.co.in/search?q=time";

                    Document document  = Jsoup.connect(url).get();
                    org.jsoup.select.Elements time = document.getElementsByClass("gsrt vk_bk FzvWSb YwPhnf");
                    org.jsoup.select.Elements date = document.getElementsByClass("KfQeJ");

                    Log.d("HTML", "google date" + String.format(date.text()));
                    Log.d("HTML", "google time" + time.text());

                    googleDate = date.text().trim();
                    googleTime = time.text().trim();
                    //'0' 在小时为一位数时不存在
                    char second = googleTime.charAt(1);
                    if(second == ':'){
                        googleTime = "0" + googleTime;
                    }
                    Log.d("Proper format", "google time" + googleTime);

                    Log.d("Date", "your current url when webpage loading.." + phoneDate);
                    Log.d("Time", "your current url when webpage loading.." + phoneTime);

                    if(googleDate.contains(phoneDate) && googleTime.equals(phoneTime)){
                        Log.d("Time", "your current url when webpage loading.." + " true");
                    }else{
                        Log.d("Time", "your current url when webpage loading.." + " false");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }   
}
英文:

This thing works best for my apps. I use jsoup to search the google time and gets current time and then I compare the phone time with google time. So if these time are different you can stop user using a dialogbox or alertbox to tell them the times have changed. You can implement in MainActivity to check this condition.
Here is a snippet so you get the idea more clearly.

public class HomeActivity extends AppCompatActivity {
//phoneDate and phoneTime to get current phone date and time
String phoneDate = new SimpleDateFormat("dd MMM yyyy ").format(clnd.getTime()).trim();
String phoneTime = new SimpleDateFormat("hh:mm a").format(clnd.getTime()).trim();
String googleDate;
String googleTime ;
@Override
protected void onCreate(Bundle _savedInstanceState) {
super.onCreate(_savedInstanceState);
setContentView(R.layout.home);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try  {
//URL to search time
String url = "https://www.google.co.in/search?q=time";
Document document  = Jsoup.connect(url).get();
org.jsoup.select.Elements time = document.getElementsByClass("gsrt vk_bk FzvWSb YwPhnf");
org.jsoup.select.Elements date = document.getElementsByClass("KfQeJ");
Log.d("HTML", "google date" + String.format(date.text()));
Log.d("HTML", "google time" + time.text());
googleDate = date.text().trim();
googleTime = time.text().trim();
//'0'is not present when hour is single digit 
char second = googleTime.charAt(1);
if(second == ':'){
googleTime = "0" + googleTime;
}
Log.d("Proper format", "google time" + googleTime);
Log.d("Date", "your current url when webpage loading.." + phoneDate);
Log.d("Time", "your current url when webpage loading.." + phoneTime);
if(googleDate.contains(phoneDate) && googleTime.equals(phoneTime)){
Log.d("Time", "your current url when webpage loading.." + " true");
}else{
Log.d("Time", "your current url when webpage loading.." + " false");
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}	
}

huangapple
  • 本文由 发表于 2020年8月20日 19:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63503891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定