Android perform action when Curren time is match to entered time(got from json)

231 Views Asked by At

i developed one application in that i got time from the json, i want to compare this time with current time ,

if both entered time (got from the json) and current time is match, then perform some action, means at that time i would want to get message both time match.

it do simply by if , but when my application is off/close at that time also, i want to receive message "both match",

How i got message when application off , and how to i compare for that, please help me to do that,

i try with alaram managaer but i dont have idea to how to compare json time and current time on that,

my time format is hh:mm like 11:10

please give me good solution to do that.

thank in advance.

Code : for getting time from the 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();
            }

i got time in j_pref_bkTime with current time i got current time from the

           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.

          }
1

There are 1 best solutions below

0
On

I don't think it is necessary to check if the given time is equal to the current time. Instead when you get your JSON, make an alarm on the time of the JSON. Then show a notification when the alarm goes off. Android will automatically check if the alarm time (so the JSON time) is equal to the current time. If you make an alarm and notification it will work even if your application is not running.