【问题标题】:Android perform action when Curren time is match to entered time(got from json)当前时间与输入时间匹配时Android执行操作(从json获取)
【发布时间】:2014-11-24 07:56:51
【问题描述】:

我开发了一个应用程序,我从 json 获得时间,我想将这个时间与当前时间进行比较,

如果输入的时间(从 json 获取)和当前时间都匹配,则执行一些操作,这意味着那时我想得到消息 两个时间都匹配

它只是通过 if 来完成的,但是当我的应用程序当时也关闭/关闭时,我想收到消息“两者都匹配”,

申请关闭时我如何收到消息,以及我如何比较,请帮我这样做,

我尝试使用 alaram managaer,但我不知道如何比较 json 时间和当前时间,

我的时间格式是 hh:mm 比如 11:10

请给我一个好的解决方案。

提前致谢。

代码:用于从 json url 获取时间

            JSONObject obj = new JSONObject(result);
            if (obj.getString("status").equals("success")) {
                 JSONArray arr = obj.getJSONArray("response");
                 UserNotificationTime jour;
                 for (int i = 0; i < arr.length(); i++) {
                    obj = arr.getJSONObject(i);
                    jour = new UserNotificationTime();
                    jour.set_breakfast_time(obj.getString("breakfast_time"));
                    j_pref_bkTime = jour.get_breakfast_time();
                    j_editor.putString("j_bktime", j_pref_bkTime);
                    j_editor.commit();
               }
            } else {
                Toast.makeText(getApplicationContext(),
                        obj.getString("response"), Toast.LENGTH_SHORT)
                        .show();
            }

我在 j_pref_bkTime 中获得了当前时间的时间 我得到了当前时间

           j_pref_bkTime = prefs.getString("j_bktime", "OFF"); // get json time from the shared preference


           int hours = new Time(System.currentTimeMillis()).getHours();
           int minute = new Time(System.currentTimeMillis()).getMinutes();

           String time = hours + ":" + minute;

           //  splits json time (hour and minute to comapare with current time)
            StringTokenizer tokens_bk = new StringTokenizer(j_pref_bkTime,
                    ":");
            String first_bk = tokens_bk.nextToken();
            String second_bk = tokens_bk.nextToken();
            String final_bktime = first_bk.trim() + ":" + second_bk.trim();

           if (time.trim().equals(final_bktime.trim())) {

             // perform some action here but it call when our app open and do some 
                into this but i want to call this code also when application is 
                close  or open means how to compare this code-condition to call 
                any time this body when current time match to json time.

          }

【问题讨论】:

  • 最好显示一些code
  • helo @maven 查看我编辑的问题

标签: android json timer alarm


【解决方案1】:

我认为没有必要检查给定时间是否等于当前时间。 相反,当您获得 JSON 时,请对 JSON 的时间发出警报。然后在闹钟响起时显示通知。 Android 会自动检查闹钟时间(即 JSON 时间)是否等于当前时间。 如果您发出警报和通知,即使您的应用程序没有运行,它也会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多