【发布时间】: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 查看我编辑的问题