ADD file via upload

main
pstgu9p43 7 months ago
parent d047f28999
commit 3dec2acc18

@ -0,0 +1,76 @@
package net.micode.notes.ui;
// 导入Android系统中管理闹钟的类
import android.app.AlarmManager;
// 导入Android系统中处理延迟意图的类
import android.app.PendingIntent;
// 导入Android系统中处理广播接收的类
import android.content.BroadcastReceiver;
// 导入Android系统中处理内容URI的类
import android.content.ContentUris;
// 导入Android系统中处理上下文环境的类用于访问应用程序的资源和类
import android.content.Context;
// 导入Android系统中处理意图的类用于描述要执行的动作
import android.content.Intent;
// 导入Android系统中处理数据库游标的类
import android.database.Cursor;
// 导入自定义的笔记数据类和笔记列类
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
/**
* 这个类是一个广播接收器,用于初始化闹钟。
*/
public class AlarmInitReceiver extends BroadcastReceiver {
// 数据库查询的列
private static final String [] PROJECTION = new String [] {
NoteColumns.ID, // 标签ID用于标识笔记
NoteColumns.ALERTED_DATE // 闹钟时间,用于设置闹钟触发的时间
};
// 对数据库的操作调用标签ID和闹钟时间
private static final int COLUMN_ID = 0; // 标签ID在查询结果中的索引
private static final int COLUMN_ALERTED_DATE = 1; // 闹钟时间在查询结果中的索引
/**
* 当接收到广播时,这个方法会被调用。
* @param context 上下文对象,用于访问应用程序的资源和类。
* @param intent 包含接收到的广播的Intent对象。
*/
@Override
public void onReceive(Context context, Intent intent) {
long currentDate = System.currentTimeMillis(); // 获取当前时间的毫秒值
// 查询数据库中未被提醒且类型为普通笔记的记录
// 注意这里使用了动态URI和投影以及WHERE子句来筛选符合条件的笔记
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);
// 如果查询结果不为空
if (c != null) {
if (c.moveToFirst()) { // 移动到查询结果的第一行
do {
// 获取闹钟时间
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
// 创建一个Intent用于触发AlarmReceiver
Intent sender = new Intent(context, AlarmReceiver.class);
// 为Intent设置数据标识具体的笔记
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建PendingIntent用于AlarmManager触发时识别
// 注意这里使用了getBroadcast方法因为AlarmReceiver是一个广播接收器
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 获取AlarmManager实例
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// 设置闹钟使用RTC_WAKEUP模式确保设备在指定时间被唤醒
// 这里的alertDate是闹钟触发的时间
alarmManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext()); // 移动到下一行,继续循环
}
c.close(); // 关闭Cursor释放资源
}
// 通过上述步骤,根据数据库里的闹钟时间创建闹钟机制
}
}
Loading…
Cancel
Save