1

all my code is in timer and it tik every 2 seconds

I have 3 different times

1) Time from the time picker(sTime).
2) Get Current time of my phone(cTime).
3) Expiry Time - (for creating range between timepicker's time and expiry time) (rTime).

I want to silent my phone when cTime is equal or after sTime and turn back my phone profile to normal when cTime is equal or after rTime

but i cannot achieve this. i have done my coding and i think logic is fine but why its not working. please help me in this regard.

Here is my code

AudioManager am;
am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);

try {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

    String savedTime = sHour + ":" + sMin;
    Date sTime = sdf.parse(savedTime);

    Calendar c = Calendar.getInstance();
    int currentHour = c.get(Calendar.HOUR);
    int currentMin = c.get(Calendar.MINUTE);
    String currentTime = currentHour + ":" + currentMin;

    Date cTime = sdf.parse(currentTime);

    int rangeHour = sHour;
    int rangeMin = sMin + 1;

    Date rTime = sdf.parse(rangeHour + ":" + rangeMin);

    if (cTime.after(sTime)) {
        am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        Log.e("res", "Silent Mode Timing");
    }

    if (cTime.after(rTime)) {
        am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        Log.e("res", "Normal Mode Timing");
    }
} catch (Exception ex) {
    Log.e("res", ex.toString());
}
Dhruv
  • 1,801
  • 1
  • 15
  • 27
Imran
  • 39
  • 9
  • I don't see problem in this part of code. Please, show timer tick code? – Konstantin May 17 '16 at 08:16
  • the above code is inside the function named timeCheck(int sHour, int sMin) inside the timer i just called the timeCheck() function. timer works perfect as i tested it by displaying value in logcat. but it doesnot change the state of phone i.e. silent and normal mode. – Imran May 17 '16 at 08:20
  • try change am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); to am.setRingerMode(AudioManager.RINGER_MODE_SILENT); and add into manifest – Konstantin May 17 '16 at 08:33
  • @Konstantin i have already added permission in manifest. and i tried silent as well but problem stays. – Imran May 17 '16 at 08:40
  • and you see Log shown correct? – Konstantin May 17 '16 at 09:01
  • yes it did show me the correct value but when it comes to condition checking there it fails. – Imran May 17 '16 at 17:17

2 Answers2

1

Ok, I have a suggestion. The method 'after' of Date class compare two long values in milliseconds. When you get current time milliseconds value it calculate as current year + current month + current day and hour etc. (or something like this). And when you get milliseconds of you savedTime it calculates incorrect because you not specify full Date (year, etc). I mean that you startTime incorrect when it convert in long milliseconds value in Date.after method. try to change you algorithm to work wit long timestamp, not with Date. for example current time is System.currentTimeMillis(). I hope it help. Sorry for my English

Konstantin
  • 174
  • 7
  • Hey i already thought this might be the problem because it gave me the current time with year 1970 . but i cannot solve this problem to get it to accurate time. can u please code a simple example for me. it would be very nice of u and very helpful for me. – Imran May 22 '16 at 20:02
0

Ok, try to use something like this:

private void compareTime(final String year, final String day, final String hour, final String month, final String minutes) {
    AudioManager am;
    am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
    final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:dd:mm:yy");
    final String savedTimeValue = hour + ":" + minutes + ":" + day + ":" + month + ":" + year;
    try {
        final Date savedDate = sdf.parse(savedTimeValue);
        final long savedTime = savedDate.getTime();
        final long currentTime = System.currentTimeMillis();
        final long rangeTimeMilliseconds = 1 * 60 * 1000; // 1 minute in milliseconds
        if (currentTime > savedTime) {
            am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        }
        if (currentTime > rangeTimeMilliseconds) {
            am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }

    }catch(Exception e) {
        e.printStackTrace();
    }
}
Konstantin
  • 174
  • 7