0

here is what i do and its not working ... i get time from shared preference from timepicker this time is picked by user at runtime name as first time ... and in the fragment i need to compare system current time with first time to send notification . but if condition is not working during comparison. and when i remove if condition and then notification works good problem is in compare time . it do not show me any error.

public Long firstTimeinLong;

@SuppressLint("NewApi")

@Nullable

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        getActivity().setTitle("Blood Pressure");

        View view = inflater.inflate(R.layout.fragment_blood_pressure, container, false);


    Date myDate = new Date(); // Default Value.

    Long dateTimeinLong = myDate.getTime();


    SharedPreferences prefs = getActivity().getSharedPreferences("MY_PREFS_BPTIME_CHECKS", MODE_PRIVATE);

    firstTimeinLong = prefs.getLong( "millis1",0 ); // firstTime1


    System.out.println("System Time : " + dateTimeinLong);
    System.out.println("First Time : " + firstTimeinLong);



    if (dateTimeinLong == firstTimeinLong.longValue()  )// i dont know why this is not working but i do not get any error
        {
            LGSnackbarManager.show(INFO, "first time is here in the loop");
            Intent notifyIntent = new Intent(getActivity().getApplicationContext(), NotificationBroadcastReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), 12345678, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getActivity().getApplicationContext().getSystemService(getActivity().getApplicationContext().ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),1000, pendingIntent);
        }

        return view;
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sibtain
  • 33
  • 1
  • 5
  • Are you sure you have value in `SharedPreference` on the first time? Show your `System.out.println` result please. – Harry T. Jul 09 '18 at 06:42
  • may this helps, https://stackoverflow.com/a/20542511/6016830 – buzzingsilently Jul 09 '18 at 06:44
  • yes i need to get first time by user this is the resuslt in system.out.println – Sibtain Jul 09 '18 at 06:52
  • I/System.out: System Time : 1531118700077 and this is the first time I/System.out: First Time : 1531118806737 i want that when first time match with system time then condition work and notification sends – Sibtain Jul 09 '18 at 06:54
  • you probably can't catch a moment when it is equal up to a millisecond. Try to expand interval to a second for example – Vladyslav Matviienko Jul 09 '18 at 07:08
  • my first time is user pick the time from Time picker and i convert that time to Long like this if it is wrong then please correct me – Sibtain Jul 09 '18 at 07:08
  • Calendar timeStamp = timePickerInputEditTextFirstTime.getTime(); Date date = new Date( String.valueOf( timeStamp.getTime() ) ); Date LongTime = timeStamp.getTime(); final Long millis1 = LongTime.getTime(); – Sibtain Jul 09 '18 at 07:08
  • Why are you expecting that the code inside if will be executed since you' re comparing a date/time from the past which you already stored in SharedPreferences with the current date/time? –  Jul 09 '18 at 07:10
  • then it means i dont need to compare first time with system time to send push notification ???? – Sibtain Jul 09 '18 at 07:16
  • This comparison will never succeed. I don't understand exactly what you're trying to do –  Jul 09 '18 at 07:22
  • ok my main purpose is to send notification to user when the first time comes .. – Sibtain Jul 09 '18 at 07:27
  • Remove the if statement and keep the code that sends the notification –  Jul 09 '18 at 07:32
  • i am trying now and then tell you any thing that will happen – Sibtain Jul 09 '18 at 07:37
  • yes when i remove if condition then notification works perfect .... but how do i do that when firstTime comes like if ( firstTime) then sendpush notification this is what i want please help me – Sibtain Jul 09 '18 at 07:51
  • What do you mean when firstTime comes? –  Jul 09 '18 at 07:55
  • i get the first time from an activity using Time Picker edit text and this first time i convert it to long like this final Long millis1 = LongTime.getTime(); then i save and send this time(first time ) from one activity to fragment activity and get that first time with the help of shared preference like editor.getLong and then i want to show the notification when the first time comes. this first is user pick the time using time picker – Sibtain Jul 09 '18 at 08:01
  • first time is a time picker edit text this is final String firsttime = timePickerInputEditTextFirstTime.getText().toString(); – Sibtain Jul 09 '18 at 08:02

1 Answers1

0

thank you all of you respectful engineers i really like that you all respond to my problem quickly ... i solved my problem with these four links this 4 links helped me in my case and i do not need to compare with the system current time.

4 -- Best way to compare dates in Android 3 -- How to repeat notification daily on specific time in android through background service 2 -- Getting hours,minutes, and seconds from Date? 1 -- How to save and retrieve Date in SharedPreferences

first i use

Calendar cal1 = Calendar.getInstance();

and set the user difined time prefs.getLong value in cal1 like this

cal1.setTimeInMillis(prefs.getLong(key"millis1", defValue 0));

and then i get the (saved time that i set to ) cal1 like this

Date firstTimeinLong = cal1.getTime();

and now i split my time like this

int minutes1 = cal1.get(Calendar.MINUTE);
int hour1 = cal1.get(Calendar.HOUR_OF_DAY);
int second1 = cal1.get(Calendar.SECOND);

and the i use another calendar object for notification and set the MINUTE, HOUR_OF_DAY and SECOND like this

Calendar cal2forNotify = Calendar.getInstance();
cal2forNotify. calendarforNotification.set(Calendar.HOUR_OF_DAY, hour1);
cal2forNotify.set(Calendar.MINUTE, minutes1);
cal2forNotify.set(Calendar.SECOND, 0);

and last change is in the alarmManger.setRepeating line like this

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendarforNotification.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

here is the Code solution code is in the image

Sibtain
  • 33
  • 1
  • 5