0

我设置了推送警报。但它不能正常工作。

有时警报会比我设置的晚几分钟。

此外,当我在 20 分钟后放置时,它根本不会让我感到震惊。

        private void setAlarm() {
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(this, AlertReceiver.class);
            intent.putExtra("supplementsId", supplements.getSupplementsId());
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode++, intent, 0);

            Calendar c = Calendar.getInstance();
            c.set(Calendar.HOUR_OF_DAY, hour);
            c.set(Calendar.MINUTE, minute);
            c.set(Calendar.SECOND, 0);

            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        }


    //[AlertReceiver.class]
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android:intent.action.BOOT_COMPLETED")) {
            id = intent.getLongExtra("supplementsId", -1);
            this.context = context;

            getSupplements();

            setNotification();
        }
    }

        private void setNotification() {
            NotificationHelper notificationHelper = new NotificationHelper(context, supplements);
            NotificationCompat.Builder notificationBuilder = 
            notificationHelper.getChannelNotification(supplements.getTitle(), "약 먹을 시간입니다.");
            notificationHelper.getManager().notify((int) supplements.getSupplementsId(), 
            notificationBuilder.build());
        }

4

1 回答 1

0

我认为可以通过唤醒屏幕来解决问题。如果不能解决请参考https://developer.android.com/training/scheduling/alarms

于 2021-09-25T13:38:57.790 回答