提前几分钟安排本地通知

kyxcudwk  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(344)

这是祈祷时间,当地通知。我们将收到通知,当它将匹配祈祷时间。问题是,总是迟到几分钟。所以为了克服这个问题,我想提前几分钟通知你。
startNotificationReceiver.class类

for (int i=0; i<prayerNames.size(); i++){
                String prayerName = prayerNames.get(i);
                if (prayerName.equals("Fajr")) {
                    String time = prayerTimes.get(0);
                    String[] array = time.split(":");
                    int hour = Integer.parseInt(array[0]);
                    String timeMinuit = array[1];
                    String[] array2 = timeMinuit.split(" ");
                    int minuit = Integer.parseInt(array2[0]);
                    String finalNotificationText = "View namaz time in " + locality + " location - " + prayerTimes.get(0);
                    Calendar calNow = Calendar.getInstance();
                    Calendar calSet = (Calendar) calNow.clone();
                    calSet.set(Calendar.HOUR_OF_DAY, hour);
                    calSet.set(Calendar.MINUTE, minuit);
                    calSet.set(Calendar.SECOND, 0);
                    calSet.set(Calendar.MILLISECOND, 0);
                    calendars.add(calSet);
                    notificationTextArrayList.add(finalNotificationText);
                }

NotificationReceiver.class类

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                        NotificationChannel mChannelFajr = notificationManager.getNotificationChannel(FAJR_CHANNEL_ID);

                        if (mChannelFajr == null) {
                            mChannelFajr = new NotificationChannel(FAJR_CHANNEL_ID, fajrChannelName, importance);
                            mChannelFajr.setDescription(notificationText);
                            mChannelFajr.enableVibration(true);
                            mChannelFajr.enableLights(true);
                            mChannelFajr.setLockscreenVisibility(VISIBILITY_PUBLIC);
                            mChannelFajr.setSound(soundUri, audioAttributes);
                            notificationManager.createNotificationChannel(mChannelFajr);
                        }

                        builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);

                        builder.setContentTitle("FAJR NAMAZ")  // required
                                .setSmallIcon(R.mipmap.ic_darshika_logo) // required
                                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                                        R.mipmap.ic_darshika_logo))
                                .setContentText(notificationText) // required
                                .setDefaults(Notification.DEFAULT_ALL)
                                .setAutoCancel(true)
                                .setLights(Color.MAGENTA, 500, 500)
                                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                                .setSound(soundUri)
                                .setContentIntent(pendingIntent)
                                .setTicker(context.getString(R.string.app_name));

                    } else {

                        builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);

                        builder.setContentTitle("FAJR NAMAZ")
                                .setSmallIcon(R.mipmap.ic_darshika_logo) // required
                                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                                        R.mipmap.ic_darshika_logo))
                                .setContentText(notificationText)  // required
                                .setDefaults(Notification.DEFAULT_ALL)
                                .setAutoCancel(true)
                                .setSound(soundUri)
                                .setLights(Color.MAGENTA, 500, 500)
                                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                                .setContentIntent(pendingIntent)
                                .setTicker(context.getString(R.string.app_name))
                                .setPriority(Notification.PRIORITY_HIGH);
                    }
                    notification = builder.build();
                    notificationManager.notify(0, notification);

这是生成通知的地方。

if (targetCal.getTimeInMillis()>System.currentTimeMillis()) {
                int requestCode = (i) * 100;
                Intent intent = new Intent(context, NotificationReceiver.class);
                intent.setAction("com.shiamomin.sjmmj.MY_NOTIFICATION");
                intent.putExtra("notificationText", finalNotificationText);
                intent.putExtra("requestCode", requestCode);
                intent.putExtra("notificationType", notificationType);
                Log.d("notificationType", String.valueOf(notificationType));
                // Loop counter `i` is used as a `requestCode`
                PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                        requestCode,
                        intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
                mgrAlarm.set(AlarmManager.RTC_WAKEUP,targetCal.getTimeInMillis(),
                        pendingIntent);
                Log.d("time", String.valueOf(mgrAlarm));

                Log.d("targetTime", String.valueOf(targetCal.getTime()));

                intentArray.add(pendingIntent);

我应该做些什么改变。

p3rjfoxz

p3rjfoxz1#

避免使用日历

嗯,我不做android编程,看起来你还没有给出足够的信息,但我可以告诉你这么多:可怕的 Calendar 类在几年前被现代的java.time类所取代。
要获得jvm当前默认时区的当前时刻,请使用 ZonedDateTime 打电话给我 now() .

ZonedDateTime nowZdt = ZonedDateTime.now() ;  // Current moment as seen in the JVM’s current default time zone.

将您的时间值跟踪为 LocalTime 对象而不是文本。

LocalTime localTime = LocalTime.of( 15 , 00 ) ;

将当前时刻调整为一天中的那个时间。

ZonedDateTime nextPrayerZdt = nowZdt.with( localTime ) ;

注意java.time使用不可变对象。而不是改变(变异)原始对象,我们生成一个新的对象,其值基于原始对象。
确保这个新的时刻在未来。

boolean isFuture = nextPrayerZdt.isAfter( nowZdt ) ;

如果你想提前几分钟,减去。

ZonedDateTime beforeNextPrayerZdt = nextPrayerZdt.minusMinutes( 3 ) ;

再次检查这是在未来。

boolean isFuture = beforeNextPrayerZdt.isAfter( nowZdt ) ;

如果必须与尚未更新到java.time的旧代码进行接口,则可以在遗留类和现代类之间来回转换。寻找新的 to… 以及 from… 方法添加到旧类中-至少在常规java中,我不知道android。请参见:
GregorianCalendar::toZonedDateTime GregorianCalendar.fromZonedDateTime ##关于java.time
java.time框架内置于Java8及更高版本中。这些类取代了旧的遗留日期时间类,例如 java.util.Date , Calendar , & SimpleDateFormat .
要了解更多信息,请参阅oracle教程。和搜索堆栈溢出的许多例子和解释。规格为jsr 310。
现在处于维护模式的joda time项目建议迁移到java.time类。
您可以直接与数据库交换java.time对象。使用符合jdbc 4.2或更高版本的jdbc驱动程序。不需要线,不需要线 java.sql.* 班级。hibernate5和jpa2.2支持java.time。
从哪里获得java.time类?
JavaSE8、JavaSE9、JavaSE10、JavaSE11及更高版本—标准JavaAPI的一部分,带有捆绑实现。
Java9带来了一些小的特性和修复。
java se 6和java se 7
大部分java.time功能都是通过三个十个后端口后端口移植到Java6和Java7的。
安卓
android(26+)的更高版本捆绑了java.time类的实现。
对于早期的android(<26),一个称为api desugaring的过程带来了java.time功能的一个子集,这些功能最初没有内置到android中。
如果desugaring不能提供您所需要的,那么threetenabp项目将threeten backport(如上所述)改编为android。了解如何使用threetenabp…。

相关问题