首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Notification AlarmManager问题

Notification AlarmManager问题
EN

Stack Overflow用户
提问于 2013-06-20 17:53:22
回答 1查看 325关注 0票数 0

我有三个问题:

1)到目前为止,我已经实现了:

代码语言:javascript
复制
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)我想每周发送一次通知,该如何实现?我试过了:

代码语言:javascript
复制
// For Monday
calendar.set(Calendar.DAY_OF_WEEK, 2)

但我不确定这是否正确,因为我不能轻易地测试它。(我不能每次都等一个星期!) :/

更多代码,设置闹钟:

代码语言:javascript
复制
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());  
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-21 21:41:52

  1. 通知发送两次。

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

  1. 当计算已过去时,将立即推送通知。

这是documented behaviour。您将需要手动检查时间是否为过去的时间,如果是,则将其向前移动一周。

  1. 我想每周发送一次通知。

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

我不能轻易地测试它。

除了手动更改手机日期的原始方法之外,您还可以通过模拟AlarmManager类来测试应用程序(用一个检查使用正确的参数调用正确的方法的伪类替换它)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17210627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档