简体   繁体   English

通知AlarmManager问题

[英]Notification AlarmManager Problems

I have three questions: 我有三个问题:

1) I have implemented this so far: 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);
    }
}

So the notification should push once anytime between 9 and 19 o'clock. 因此,通知应该在9点至19点之间的任何时间推送一次。 But the notification is sent twice between 9 and 19 o'clock. 但是通知在9点至19点之间发送了两次。

2) When the calculation is in the past, the notification is pushed right away. 2)计算过去时,将立即推送通知。

3) I want to send a notification once a week, how do I Implement this? 3)我想每周发送一次通知,该如何实施? I have tried: 我努力了:

// For Monday
calendar.set(Calendar.DAY_OF_WEEK, 2)

But I am not sure if this is right, because I can't test this easily. 但是我不确定这是否正确,因为我无法轻松进行测试。 (I cannot wait a week everytime!) :/ (我不能每次等一个星期!):/

Some more Code, to set the alarm: 一些更多的代码,以设置警报:

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());  
        }
    }
  1. The notification is sent twice. 通知发送两次。

    Seeing the code that actually interacts with the AlarmManager would be helpful. 查看实际与AlarmManager交互的代码将很有帮助。 If you display the selected time (using for example the logging facility) you will be able to see which notification is the right one and if the code that schedules the alarm is being called twice. 如果显示选定的时间(例如使用日志记录工具),您将能够看到哪个通知是正确的通知,并且调度警报的代码是否被调用了两次。

  2. When the calculation is in the past, the notification is pushed right away. 计算过去时,将立即推送通知。

    This is the documented behaviour . 这是记录的行为 You will need to manually check if the time is in the past, and move it forward by a week if it is. 您将需要手动检查时间是否过去了,如果已经过去,则将其提前一周。

  3. I want to send a notification once a week. 我想每周发送一次通知。

    Try setRepeating(type, start, 7 * AlarmManager.INTERVAL_DAY, operation) . 尝试setRepeating(type, start, 7 * AlarmManager.INTERVAL_DAY, operation)

    I can't test this easily. 我无法轻松测试。

    Besides the primitive method of changing the phone's date manually, you can test the app by mocking the AlarmManager class (replace it with a dummy class that checks the right methods are called with the right parameters). 除了手动更改电话日期的原始方法外,您还可以通过模拟AlarmManager类来测试该应用程序(将其替换为虚拟类,该虚拟类检查使用正确的参数调用正确的方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM