【问题标题】:compare two time in fragment is not working在片段中比较两次不起作用
【发布时间】:2018-07-09 06:36:09
【问题描述】:

这是我所做的,但它不起作用......我从 timepicker 的共享偏好中获取时间,这次是由用户在运行时名称作为第一次选择......并且在片段中我需要将系统当前时间与第一次发送通知。但如果在比较过程中条件不起作用。当我删除 if 条件然后通知工作正常时,问题在于比较时间。它没有显示任何错误。

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;
    }

【问题讨论】:

  • 你确定你第一次在SharedPreference中有价值吗?请显示您的System.out.println 结果。
  • 这可能会有所帮助,stackoverflow.com/a/20542511/6016830
  • 是的,我需要第一次由用户获取,这是 system.out.println 中的结果
  • I/System.out: System Time : 1531118700077 这是第一次 I/System.out: First Time : 1531118806737 我希望当第一次与系统时间匹配时条件工作和通知发送
  • 你可能无法捕捉到一个毫秒的时刻。例如尝试将时间间隔扩大到一秒

标签: android time push-notification compare


【解决方案1】:

谢谢你们所有尊敬的工程师我真的很喜欢你们都快速响应我的问题......我用这四个链接解决了我的问题这四个链接在我的情况下帮助了我,我不需要与系统进行比较当前时间。

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

第一次使用

Calendar cal1 = Calendar.getInstance();

并像这样在cal1中设置用户定义的时间prefs.getLong

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

然后我得到(我设置为的保存时间)cal1 像这样

Date firstTimeinLong = cal1.getTime();

现在我就这样分配时间

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

我使用另一个日历对象进行通知,并像这样设置 MINUTE、HOUR_OF_DAY 和 SECOND

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

最后的变化是在这样的alarmManger.setRepeating 行中

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

这里是代码解决方案 code is in the image

【讨论】:

    猜你喜欢
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多