You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
read/AlarmInitReceiver-note.java

89 lines
4.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.ui;
// 定义当前类所属的包,通常用于组织代码并避免命名冲突
import android.app.AlarmManager;
// 导入Android的AlarmManager类用于设置和管理闹钟
import android.app.PendingIntent;
// 导入Android的PendingIntent类用于在Future时刻发送意图Intent
import android.content.BroadcastReceiver;
// 导入Android的BroadcastReceiver类用于接收广播消息
import android.content.ContentUris;
// 导入Android的ContentUris类用于处理内容提供者的URI
import android.content.Context;
// 导入Android的Context类提供应用程序环境的描述
import android.content.Intent;
// 导入Android的Intent类用于在应用组件之间传递信息
import android.database.Cursor;
// 导入Android的Cursor类用于访问查询结果
import net.micode.notes.data.Notes;
// 导入应用程序的Notes类表示笔记的数据模型
import net.micode.notes.data.Notes.NoteColumns;
// 导入Notes类中的列定义用于表明笔记的数据库字段
public class AlarmInitReceiver extends BroadcastReceiver {
// 定义类 AlarmInitReceiver继承自 BroadcastReceiver用于处理广播接收
private static final String [] PROJECTION = new String [] {
// 定义一个字符串数组 PROJECTION指定要查询的列
NoteColumns.ID, // 笔记的 ID
NoteColumns.ALERTED_DATE // 笔记的提醒日期
};
private static final int COLUMN_ID = 0; // 定义常量 COLUMN_ID表示 ID 列的索引
private static final int COLUMN_ALERTED_DATE = 1; // 定义常量 COLUMN_ALERTED_DATE表示提醒日期列的索引
@Override
public void onReceive(Context context, Intent intent) {
// 当接收到广播时调用此方法
long currentDate = System.currentTimeMillis(); // 获取当前时间的毫秒值
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
// 通过内容解析器查询笔记内容提供者,获取光标
PROJECTION, // 指定要查询的列
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE, // 查询条件:提醒日期大于当前时间且类型为笔记
new String[] { String.valueOf(currentDate) }, // 查询参数:当前时间的字符串形式
null);//查询时的排序参数,这里设置为 null表示使用默认排序。
if (c != null) {
//检查返回的 Cursor 是否为空,确保查询结果有效。
if (c.moveToFirst()) {
//如果 Cursor 能移动到第一行,表示查询到有结果。
do {
//开始循环处理每一行的查询结果。
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
//获取当前行的提醒日期,并将其赋值给 alertDate 变量。
Intent sender = new Intent(context, AlarmReceiver.class);
//创建一个 Intent 对象,指定要发送到的接收者 AlarmReceiver。
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
//设置 Intent 的数据为笔记的 URI使用 ContentUris 类附加当前行的笔记 ID。
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
//创建一个 PendingIntent用于将来发送广播传递刚创建的 Intent。
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//获取系统的 AlarmManager 服务,以便设置闹钟。
alarmManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
//使用 AlarmManager 设置一个闹钟,指定为 RTC_WAKEUP 模式,设置该闹钟的提醒时间为 alertDate并关联到之前创建的 PendingIntent。
}while (c.moveToNext());
//移动到 Cursor 中的下一行,继续处理下一条查询结果,直到没有更多行。
}
c.close();
//关闭 Cursor释放与其相关联的资源防止内存泄漏。
}
}
}