Android:在应用程序停止时检测用户是否更改系统时间的方法

时间:2015-06-05 10:26:26

标签: android time notifications

我正在为某家公司内部开发Android应用程序,并且需要记录员工的工作时间。因此,系统时间的工作至关重要。我的应用程序非常需要知道用户何时更改系统时间。很重要,你说,看到这个:Is there a way to detect when the user has changed the clock time on their device?

问题是用户可能会在更改时间之前通过强制停止应用程序来绕过该解决方案。然后,该应用程序无法接收系统通知,此处将对此进行精彩描述:https://stackoverflow.com/a/19856367/1309803

我不介意在下次启动应用程序时检查,但我怎么可能知道用户是否更改了时间?我了解SystemClock.elapsedRealtime()。如果用户没有重启设备,我可以根据这些值的增量来计算时移,但这是我不确定的。我的应用程序订阅了BOOT_COMPLETED事件,但是当应用程序处于停止状态时,不会收到该事件。

而且,最重要的是,该公司的员工应该在没有网络访问的情况下工作,所以我不能依赖Web服务器。那么还有其他可能的方法吗?

1 个答案:

答案 0 :(得分:0)

在大多数情况下,从第三方服务器获取时间并不可靠,其中一些是付费服务。

如果要获取准确的时间并用手机检查时间是否正确,不管使用哪种正确的方法,都可以使用以下简单的技巧来获取实际时间。 / p>

private class GetActualTime extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {
            try {
                HttpURLConnection urlConnection = null;
                StringBuilder result = new StringBuilder();
                try {
                    URL url = new URL(urls[0]);
                    urlConnection = (HttpURLConnection) url.openConnection();
                    int code = urlConnection.getResponseCode();
                    if (code == 200) {
                        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                        String line = "";
                        while ((line = bufferedReader.readLine()) != null)
                        result.append(line);
                        in.close();
                    }
                        else {
                        return "error on fetching";
                    }
                    return result.toString();
                } catch (MalformedURLException e) {
                    return "malformed URL";
                } catch (IOException e) {
                   return "io exception";
                } finally {
                    if (urlConnection != null) {urlConnection.disconnect();
                    }
                }
            } catch (Exception e) { return "null"; }
        }

        @Override
        protected void onPostExecute(String time) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat mdformat = new SimpleDateFormat("h:mm");
            String times =  mdformat.format(calendar.getTime());
            try {
                String areatime = time.substring(time.indexOf(String.valueOf(times)), time.indexOf(String.valueOf(times)) + 5).trim(); 
                        Toast.makeText(this, "The actual time is " + areatime, Toast.LENGTH_SHORT).show();

            }
            catch(IndexOutOfBoundsException e){
                        Toast.makeText(this, "Mobile time is not same as Internet time", Toast.LENGTH_SHORT).show();
                }
            }


        }

    }

在onCreate()中调用该类;

new GetActualTime().execute("https://www.google.com/search?q=time");

因此,这实际上是从Google获得时间。这在我的项目中非常出色。为了检查系统时间是否错误,可以使用此技巧。您不必依赖时间服务器,而可以信任Google。

由于检查更为敏感,因此即使提前一分钟或滞后一分钟也会捕获异常。您可以自定义代码。