java - 在特定日期和时间创建警报

标签 java android events notifications alarm

我正在尝试让闹钟或通知在特定时间和日期响起。我希望能够为数组中的特定日期设置多个警报。我不知道从哪里继续让我的按钮正常工作:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Button button = (Button)findViewById(R.id.button1);
   // button.setOnClickListener(this);
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
    final Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v) {

             if (checkBox.isChecked()) {



             }

        }

预先感谢您对我的帮助。

最佳答案

您将要使用 Alarm Manager

tutorial是一个如何使用它的非常简单的示例。

另一个简单的exmaple将其分解为基本步骤

  1. 创建 Activity
  2. 创建一个 BroadcastReceiver 来处理事件

编辑
要发出通知,请修改广播接收器以在收到警报事件时创建通知。

Android Status Notification Documentation很好地展示了创建通知的基本步骤。

  1. 获取对 NotificationManager 的引用
  2. 创建通知实例。然后您可以定义图像、文本和时间,这只是当前时间,因为警报事件已经定义了何时关闭
  3. 定义通知内容,例如消息和点击时启动的 Intent
  4. 调用通知

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) receiverContext.getSystemService(ns);

Notification notification = new Notification(R.drawable.notification_icon, "Hello", System.currentTimeMillis());

CharSequence contentTitle = "My notification";  
CharSequence contentText = "Hello World!";  
Intent notificationIntent = new Intent(receiverContext, TargetActivity.class);  
PendingIntent contentIntent = PendingIntent.getActivity(receiverContext, 0, notificationIntent, 0);  

notification.setLatestEventInfo(receiverContext, contentTitle, contentText, contentIntent);  

private static final int notificationID = 1;

mNotificationManager.notify(notificationID, notification);

编辑 2
Creating notification at specified time using Alarm Manager

关于java - 在特定日期和时间创建警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11905715/

相关文章:

java - 排序 2 个数组

android - 为什么我的 SharedPreferences 在单击按钮时不保存?

jquery - 在文本区域的新行上触发 jQuery 事件(不是键盘输入)

c - XCB的事件处理如何检测C中的ESC键?

Java多线程控制

java - 如何从命令行将 VM 参数传递给 gradle?

java - 替换 sun.security.* 包中的类

使用 Google Plus : How do I GoogleApiClient. 再次连接的 Android 设计?

android - 如何在旧版 Android 上加载分辨率的特定布局

c# - SerialPort_DataReceived 事件的问题