通知会忽略警报设置并立即开始

时间:2018-10-20 07:50:26

标签: android notifications alarmmanager android-notifications android-alarms

我正在尝试在特定时间弹出通知,但由于我设置了警报,因此它会立即忽略给定时间

public class ShowTaskNotificationService extends IntentService {
private Long date_days;
private Long date_time;

public ShowTaskNotificationService() {
    super("ShowTaskNotificationService");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
     date_days = intent.getLongExtra("Date_days",-1);
     date_time = intent.getLongExtra("Date_time",-1);
     return super.onStartCommand(intent, flags, startId);
}

@Override
protected void onHandleIntent(Intent intent) {
    startAlarm();
}
private void startAlarm()
{
    int year,month,day,hour,minute;
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

    Calendar calendar_days = Calendar.getInstance();
    calendar_days.setTimeInMillis(date_days);

    Calendar calendar_time = Calendar.getInstance();
    calendar_time.setTimeInMillis(date_time);



    year   = calendar_days.get(Calendar.YEAR);
    month  = calendar_days.get(Calendar.MONTH);
    day    = calendar_days.get(Calendar.DAY_OF_MONTH);
    hour   = calendar_time.get(Calendar.HOUR_OF_DAY);
    minute = calendar_time.get(Calendar.MINUTE);

    //Date date = new Date();
    Calendar cal_alarm = Calendar.getInstance();
    cal_alarm.setTimeInMillis(System.currentTimeMillis());
    //cal_alarm.clear();
    //cal_alarm.setTime(date);
    cal_alarm.set(Calendar.YEAR,year);
    cal_alarm.set(Calendar.MONTH,month);
    cal_alarm.set(Calendar.DAY_OF_MONTH,day);
    cal_alarm.set(Calendar.HOUR_OF_DAY,hour);
    cal_alarm.set(Calendar.MINUTE,minute);


    Intent intent = new Intent(this,AlarmReceiver.class);
    intent.putExtra("cal_alarm",cal_alarm.getTimeInMillis());
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent,0);
    alarm.set(AlarmManager.RTC_WAKEUP,cal_alarm.getTimeInMillis(),pendingIntent);


}

}

公共类AlarmReceiver扩展了BroadcastReceiver {         私有静态字符串CHANNEL_ID =“ taskNoti”;

@Override
    public void onReceive(Context context, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = "channel name";
                String description = "channel description";
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
                channel.setDescription(description);
                NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Task time")
                    .setContentText("aaa")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

            notificationManager.notify(12344, mBuilder.build());
            mBuilder.build();

        }

}

public class AlarmReceiver extends BroadcastReceiver {
    private static String CHANNEL_ID = "taskNoti";

@Override
    public void onReceive(Context context, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = "channel name";
                String description = "channel description";
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
                channel.setDescription(description);
                NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Task time")
                    .setContentText("aaa")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

            notificationManager.notify(12344, mBuilder.build());
            mBuilder.build();

        }

}

我从另一个片段中获取时间,并将其传递给ShowTaskNotificationService类,然后将其作为Long变量(date_days,date_time)获取

1 个答案:

答案 0 :(得分:0)

您要设置什么时间?
here所述,如果您指定的触发时间是过去的时间,则警报将立即触发。这就是您的情况。在cal_alarm中指定与当前时间相同的时间。
选择您希望闹钟响起的时间。像这样:

// Set the alarm to start at approximately 2:00 p.m.  
Calendar calendar = Calendar.getInstance();  
calendar.setTimeInMillis(System.currentTimeMillis());  
calendar.set(Calendar.HOUR_OF_DAY, 14);    
// Set the alarm to start at 8:30 a.m.  
Calendar calendar = Calendar.getInstance();  
calendar.setTimeInMillis(System.currentTimeMillis());  
calendar.set(Calendar.HOUR_OF_DAY, 8);