Java 警报管理器问题通知

Java 警报管理器问题通知,java,android,algorithm,notifications,alarmmanager,Java,Android,Algorithm,Notifications,Alarmmanager,我有三个问题: 1) 到目前为止,我已经实施了以下措施: public final class CalculateTime { public int iHour; public int iMinute; public Calendar calendar; private void calculateRandomNumbers() { Random randomNumberGenerator = new Random(); iHo

我有三个问题:

1) 到目前为止,我已经实施了以下措施:

public final class CalculateTime {

    public int iHour;
    public int iMinute;
    public Calendar calendar;

    private void calculateRandomNumbers() {
        Random randomNumberGenerator = new Random();
        iHour = randomNumberGenerator.nextInt(19 - 9) + 9;
        iMinute = randomNumberGenerator.nextInt(59);
    }

    public void calculateRandomTime() {
        calculateRandomNumbers();
        calendar = GregorianCalendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, iHour);
        calendar.set(Calendar.MINUTE, iMinute);
    }
}
因此,通知应该在9点到19点之间的任何时间推送一次。但是通知在9点到19点之间发送了两次

2) 当计算在过去时,会立即推送通知

3) 我想每周发送一次通知,如何实现?我试过:

// For Monday
calendar.set(Calendar.DAY_OF_WEEK, 2)
但我不确定这是否正确,因为我无法轻松测试。(我不能每次都等一个星期!):/

还有一些代码,用于设置报警:

public final class NotificationService extends IntentService {

    public static final String DESCRIPTION = "description";
    public static final String HEADLINE = "headline";

    private static final int FM_NOTIFICATION_ID = 0;

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

    @Override
    protected void onHandleIntent(Intent intent) {
        /*
         * This method is invoked whenever an intent for this service is fired.
         * The actual firing is done by the AlarmManager in the
         * TodoEntriesAdapter, but really we don't care at this point where
         * exactly the intent came from.
         */
        String description = intent.getStringExtra(DESCRIPTION);
        String headline = intent.getStringExtra(HEADLINE);

        addNotification(description, headline);
    }

    private void addNotification(String description, String headline) {

        NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(headline)
            .setContentText(description)
            .setAutoCancel(true);

        Intent notificationIntent = new Intent(this, ReadNotification.class);
        notificationIntent.putExtra("description", description);
        notificationIntent.putExtra("headline", headline);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,   
            PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(contentIntent);

        // Add as notification  
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
        manager.notify(FM_NOTIFICATION_ID, builder.build());  
        }
    }
  • 通知发送两次

    查看与
    AlarmManager
    实际交互的代码会很有帮助。如果显示选定的时间(例如,使用日志记录功能),您将能够看到哪个通知是正确的,以及安排报警的代码是否被调用两次

  • 当计算在过去时,会立即推送通知

    这是最新的。您需要手动检查时间是否在过去,如果在过去,则向前移动一周

  • 我想每周发一次通知

    尝试设置重复(键入、开始、7*AlarmManager.INTERVAL\u DAY、操作)

    我不能轻易地测试这个

    除了手动更改手机日期的基本方法外,您还可以通过模拟AlarmManager类来测试应用程序(将其替换为检查使用正确参数调用正确方法的虚拟类)


  • nextInt(x)
    返回一个介于0到x-1之间的整数,而不是0到x之间的整数。请您将代码张贴在您实际设置报警的位置好吗?@tom No,它介于0到x-1之间(包括两者)@MichaelButscher谢谢(更正)。我不知道这是怎么发生的。我已经用更多的代码编辑了我的帖子,我希望这会有所帮助