0

我正在制作一个类似于待办事项列表的应用程序。现在,我希望我的应用程序在某一天后某个设置的“待办事项”事件时显示通知。即使应用程序未运行,我也希望弹出通知。我怎样才能做到这一点?阅读有关通知的官方开发人员文档并没有真正让我明白。

4

2 回答 2

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);

完整的源代码

于 2012-12-28T09:34:21.930 回答
1

您可以创建一个Android Service并使用wakeful intent service来安排警报,当您收到警报时,您可以显示一个警报框。

为您找到下面的代码链接:

  1. 清醒的意图
  2. Android 服务文档
于 2012-12-28T09:43:31.867 回答