【问题标题】:Setting a notification to fire at a specific time in Android将通知设置为在 Android 中的特定时间触发
【发布时间】:2012-04-22 00:08:57
【问题描述】:

我正在尝试获取在特定时间触发的通知。我突然想到我不必使用警报管理器,因为我有设置时间的“alCal”变量。是否可以将 alCal 转换为 Long 并使用它而不是 System.currentTimeMillis(); 作为 fireTime 变量?如果是这样,我将如何将 alCal 转换为 Long?这是我当前的代码:

            //---use the AlarmManager to trigger an alarm---
        AlarmManager aMan = (AlarmManager) getSystemService(ALARM_SERVICE);

            //---get current date and time---
        Calendar alCal = Calendar.getInstance();
        //---sets the time for the alarm to trigger---                       
        alCal.set(Calendar.HOUR_OF_DAY, 23);            
        alCal.set(Calendar.MINUTE, 41);                
        alCal.set(Calendar.SECOND, 00);

        Intent noteIntent = new Intent(this, Splash.class);
        PendingIntent pendI = PendingIntent.getActivity(this, 0, noteIntent, 0);
        long fireTime = System.currentTimeMillis();
        String noteBody = "notification body";
        String noteTitle = "notification title";
        Notification note = new Notification(R.drawable.noteicon, noteTitle, fireTime);
        note.setLatestEventInfo(this, noteTitle, noteBody, pendI);
        note.defaults = Notification.DEFAULT_SOUND;
        noteman.notify(uniqueID, note);
        //---sets the alarm to trigger--- 
        aMan.set(AlarmManager.RTC_WAKEUP, alCal.getTimeInMillis(), pendI); 
        finish();

好的,看起来我无法将 alCal 转换为 long,所以我想我必须使用 AlarmManager。那么给定上面的代码,我该如何修改它以便AlarmManger在指定的时间触发通知?我是 Android 和 Java 的新手,所以我并没有真正看到这个问题。我认为必须在新的通知调用中调用 alCal,但我不知道该怎么做。 Eclipse 说我不能把 fireTime 换成 alCal,所以我不知道下一步该尝试什么。

【问题讨论】:

  • 正在使用alCal.getTimeInMillis将alCal“转换”为long...您的问题是什么?
  • 您在 2 小时前发布了类似的问题,我回答了。您是否尝试过该解决方案?我现在没有看到这个问题。你删了吗?
  • 是的,我确实删除了这个问题,因为我认为这是一个更好的问题。但也许不是。我确实尝试了您的解决方案,并在其中放置了一个日期变量使应用程序崩溃。

标签: android android-notifications notificationmanager


【解决方案1】:

我突然想到我不必使用警报管理器

是的,你会的。

因为我有设置时间的“alCal”变量。

这是一个java.util.Calendar 对象。它本身不会在特定时间执行您的代码(例如显示Notification)。

是否可以将 alCal 转换为 Long 并使用它来代替 System.currentTimeMillis();作为 fireTime 变量?如果是这样,我将如何将 alCal 转换为 Long?

致电getTimeInMillis()

请记住,您的代码不是“在 Android 中将通知设置为在特定时间触发”。它将立即显示Notification,并设置警报以在将来的某个时间调用活动。

【讨论】:

  • 当然,您指出的一切都是正确的。我是 Android 新手(我想你可以看出来)。我正在尝试做的设置在Android中的特定时间触发通知,我已经从各个地方获得了上述大部分代码,你是对的,这段代码的作用是立即触发通知然后设置闹钟。您能告诉我如何修改代码以使警报将通知设置为触发吗?我想我必须将 AlCal 变量放入新的 Notification 调用中,但我不知道该怎么做。
  • @kirktoon1882:“你能告诉我如何修改代码,以便警报将通知设置为触发吗?” -- 使用 PendingIntentAlarmManager 调用其他东西(例如,BroadcastReceiver),进而引发Notification
  • 我查看了mobiforge.com/developing/story/…,但对于我想做的事情来说,它看起来过于复杂。本教程允许用户设置警报。我不想那样做。我只想给一个静态时间来触发通知。将警报管理器、通知和广播接收器都放在同一个活动类中是不是不好的编码?本教程包含三个课程,这对我来说似乎有点过头了。
  • @kirktoon1882:BroadcastReceiver 必须在一个单独的类中,除非您只希望在活动处于前台时引发 Notification - 您需要您的 BroadcastReceiver在清单中注册。 Notification 不是您编写的类,而是来自您使用的操作系统的类。 AlarmManager 也是如此。
猜你喜欢
  • 2012-12-23
  • 1970-01-01
  • 2011-05-12
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多