【问题标题】:Android notification at exact time准确时间的 Android 通知
【发布时间】:2018-01-23 23:43:59
【问题描述】:

对于我的一生,我无法弄清楚为什么我不能为 android 通知设置确切的时间。这几天一直想弄明白,还是不明白为什么。

有时,它在时间选择器的完全相同的时间工作。但是,有时会迟到 20 秒、34 秒甚至整整一分钟。如果有人可以帮助我,将不胜感激,因为我不知道还能做什么。我正在将时间设置为从日期毫秒开始的警报管理器。

广播接收器:

public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    int code = intent.getIntExtra("code", 0);

    Notification notification = new NotificationCompat.Builder(context)
            .setContentTitle("Task reminder!")
            .setTicker(intent.getStringExtra("taskText"))
            .setContentText(intent.getStringExtra("taskText"))
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentIntent(PendingIntent.getActivity(context, code, new Intent(context, MainActivity.class), 0))
            .setAutoCancel(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .build();
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notification.defaults = Notification.DEFAULT_SOUND;

    notificationManager.notify(code, notification);
}

}

主活动:

int code = new Random().nextInt(1200) + 

                    AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                    Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);
                    intent.putExtra("taskText", taskInput.getText().toString());
                    intent.putExtra("code", code);
                    PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), code, intent, 0);

                    alarmMgr.set(AlarmManager.RTC, reminder, alarmIntent);

                    Intent i = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(i);

从中获得我的时间:

Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(System.currentTimeMillis());
            cal.set(Calendar.YEAR, datePicker.getYear());
            cal.set(Calendar.MONTH, datePicker.getMonth());
            cal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
            cal.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
            cal.set(Calendar.MINUTE, timePicker.getCurrentMinute());
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);

【问题讨论】:

  • 请查阅AlarmManager 的文档。自 KitKat 以来,set() 方法并不准确。
  • 嗨,迈克。我现在正在研究 setExact 方法。它现在给我一个错误:调用需要 API 级别 19(当前最小值为 15):android.app.AlarmManager#setExact
  • 您需要检查当前的 Android 版本,并为每个版本调用适当的方法。 this question 中有一个例子,在第一个代码块中的 //MARSHMALLOW OR ABOVE 注释之后。

标签: android notifications alarmmanager alarm


【解决方案1】:

修好了!将 set() 替换为 setExact()。

谢谢,迈克!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    相关资源
    最近更新 更多