java - Android 警报接收器从未触发

标签 java android date alarmmanager android-alarms

我正在尝试根据用户定义的值创建通知,但我的闹钟不起作用;它从未被触发。

fragment 代码:

// Set reminder
            // From java.util.Calendar;
            Calendar timeNow = Calendar.getInstance();
            timeNow.setTimeInMillis(System.currentTimeMillis());

            Calendar thatDate = Calendar.getInstance();
            thatDate.setTimeInMillis(System.currentTimeMillis());



            // add date and time to calender object

            //** Im trying to set the alarm for the 20th of April, 2013 at 14:03**
            thatDate.add(Calendar.MINUTE, 3 - timeNow.get(Calendar.MINUTE));
            thatDate.add(Calendar.MONTH, 3 - timeNow.get(Calendar.MONTH)); //Its April, so I did 3 not 4

            thatDate.add(Calendar.DAY_OF_MONTH,
                    20 - timeNow.get(Calendar.DAY_OF_MONTH));

            thatDate.add(Calendar.HOUR, 14 - timeNow.get(Calendar.HOUR));
            thatDate.add(Calendar.YEAR, 2013 - timeNow.get(Calendar.HOUR));


            Intent alarmintent = new Intent(getActivity(),
                    AlarmReceiver.class);
            alarmintent.putExtra("title", titleEt.getText().toString());
            alarmintent.putExtra("note", desc.getText().toString());

            PendingIntent sender = PendingIntent
                    .getBroadcast(getActivity(), ALARM_ID, alarmintent,
                            PendingIntent.FLAG_UPDATE_CURRENT
                                    | Intent.FILL_IN_DATA);

            /*
             * VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT... This will send
             * the correct extra's informations to the AlarmReceiver class
             */

            // Get the AlarmManager service

            AlarmManager am = (AlarmManager) getActivity()
                    .getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, thatDate.getTimeInMillis() - timeNow.getTimeInMillis(), sender);

还有我的警报接收器代码:

public class AlarmReceiver extends BroadcastReceiver {

private static int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationManager manger = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(
            R.drawable.ic_stat_reminder, "Todo Reminder",
            System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(context,
            NOTIFICATION_ID, new Intent(context, AlarmReceiver.class), 0);
    Bundle extras = intent.getExtras();
    String title = extras.getString("title");

    // Get the title and description of our Notification

    /*
     * Notification noti = new Notification.InboxStyle( new
     * Notification.Builder() .setContentTitle("5 New mails from " +
     * sender.toString()) .setContentText(subject)
     * .setSmallIcon(R.drawable.new_mail) .setLargeIcon(aBitmap))
     * .addLine(str1) .addLine(str2) .setContentTitle("")
     * .setSummaryText("+3 more") .build();
     */

    String note = extras.getString("note");
    notification.setLatestEventInfo(context, note, title, contentIntent);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.defaults |= Notification.PRIORITY_MAX;
    // Set the default sound for our notification

    // notification
    manger.notify(NOTIFICATION_ID++, notification);

}
};

最佳答案

仅使用thatDateset的时间是实时的,不是延迟的。

 AlarmManager am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
 am.set(AlarmManager.RTC_WAKEUP, thatDate.getTimeInMillis(), sender);

关于java - Android 警报接收器从未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16120702/

相关文章:

java - 有没有办法让这些 scaledBitmaps 占用更少的内存,这样我就不会一直进行太多的输出过程?

android - "Explicit termination method ' 关闭 ' not called"是什么意思?

R 更改月份/合并日期和年份向量

javascript - jquery ui datepicker 允许过去的日期吗?

python - Django Forms - 如何使表单输入类型等于 "date"

java - 从段落中提取给定的子字符串

java - Spring MongoDB 依赖错误

java - Spring SimpleThreadScope 未在@Components 上正确 Autowiring

php - Android 向服务器执行分析请求

java - JTextField Keylistener 无法删除输入