如何设置通知,以显示特定事件何时接近。

时间:2012-12-28 09:28:36

标签: android android-notifications

我正在创建一个类似于待办事项列表的应用程序。现在,我希望我的应用程序在某个设置的“待办事项”事件发生一天后显示通知。即使应用未运行,我也希望弹出通知。我怎样才能做到这一点? 阅读official developer document about notification对我来说并不是很清楚。

2 个答案:

答案 0 :(得分:1)

也许这可以清除一点:http://blog.blundell-apps.com/notification-for-a-user-chosen-time/

如果您想在待办事项数据前一天收到通知,那么您将从该日期起减去1天。

1)设置闹钟

 getSystemService(Context.ALARM_SERVICE).set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);

2)捕捉警报

3)显示通知

        // This is the 'title' of the notification
        CharSequence title = "Alarm!!";
        // This is the icon to use on the notification
        int icon = R.drawable.ic_dialog_alert;
        // This is the scrolling text of the notification
        CharSequence text = "Your notification time is upon us.";       
        // What time to show on the notification
        long time = System.currentTimeMillis();

        Notification notification = new Notification(icon, text, time);

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, title, text, contentIntent);

        // Clear the notification when it is pressed
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Send the notification to the system.
        mNM.notify(NOTIFICATION, notification);

Full Source Code

答案 1 :(得分:1)

您可以创建Android Service并使用wakeful intent service来安排闹钟,当您收到闹钟时,您可以显示提示栏。

您可以在下面找到代码链接:

  1. Wakeful Intent
  2. Android Service Documentation