这是我的工作,它不起作用...我从时间选择器的共享首选项中获取时间,这一次是用户在运行时名称中选择的时间...并且在片段中,我需要将系统当前时间与第一次发送通知。但如果条件在比较期间不起作用。当我删除如果条件,然后通知工作良好的问题是比较时间。它不会显示任何错误。
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;
}
答案 0 :(得分:0)
谢谢大家尊敬的工程师,我真的很喜欢你们所有人都对我的问题做出快速反应……我用这四个链接解决了我的问题,这四个链接在我的情况下对我有帮助,我不需要与系统进行比较当前时间。
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