|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
*
|
|
|
* 授权在Apache License, Version 2.0(以下简称“许可证”)下使用;
|
|
|
* 除非符合许可证要求,否则您不得使用此文件。
|
|
|
* 您可以在以下网址获得许可证副本:
|
|
|
*
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
*
|
|
|
* 除非适用法律要求或书面同意,否则根据许可证分发的软件将按“现状”分发,
|
|
|
* 不提供任何明示或暗示的担保。有关权限和许可证下限制的具体语言,请参阅许可证。
|
|
|
*/
|
|
|
|
|
|
package net.micode.notes.ui; // 指定当前类所在的包
|
|
|
|
|
|
import android.app.AlarmManager; // 导入AlarmManager类,用于设置闹钟
|
|
|
import android.app.PendingIntent; // 导入PendingIntent类,用于表示一个待执行的Intent
|
|
|
import android.content.BroadcastReceiver; // 导入BroadcastReceiver类,用于接收广播
|
|
|
import android.content.ContentUris; // 导入ContentUris类,用于处理URI
|
|
|
import android.content.Context; // 导入Context类,表示应用程序环境的全局信息
|
|
|
import android.content.Intent; // 导入Intent类,用于不同组件间的通信
|
|
|
import android.database.Cursor; // 导入Cursor类,用于访问数据库查询结果
|
|
|
|
|
|
import net.micode.notes.data.Notes; // 导入Notes类,用于访问笔记数据
|
|
|
import net.micode.notes.data.Notes.NoteColumns; // 导入NoteColumns类,包含笔记表的列名
|
|
|
|
|
|
// 定义一个继承自BroadcastReceiver的类,用于初始化闹钟
|
|
|
public class AlarmInitReceiver extends BroadcastReceiver {
|
|
|
|
|
|
// 定义一个字符串数组,用于指定查询数据库时要返回的列
|
|
|
private static final String [] PROJECTION = new String [] {
|
|
|
NoteColumns.ID, // 笔记的ID
|
|
|
NoteColumns.ALERTED_DATE // 笔记的提醒日期
|
|
|
};
|
|
|
|
|
|
// 定义常量,表示列索引
|
|
|
private static final int COLUMN_ID = 0; // 笔记ID的列索引
|
|
|
private static final int COLUMN_ALERTED_DATE = 1; // 提醒日期的列索引
|
|
|
|
|
|
// 重写onReceive方法,当接收到广播时调用
|
|
|
@Override
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
long currentDate = System.currentTimeMillis(); // 获取当前时间
|
|
|
// 查询数据库,获取所有未提醒且类型为笔记的记录的ID和提醒日期
|
|
|
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 sender = new Intent(context, AlarmReceiver.class); // 创建一个Intent,用于唤醒AlarmReceiver
|
|
|
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID))); // 设置Intent的URI,包含笔记ID
|
|
|
// 获取PendingIntent,用于AlarmManager唤醒时执行
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
|
|
|
AlarmManager alermManager = (AlarmManager) context
|
|
|
.getSystemService(Context.ALARM_SERVICE); // 获取AlarmManager服务
|
|
|
// 设置闹钟,在提醒日期时唤醒AlarmReceiver
|
|
|
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
} while (c.moveToNext()); // 移动到下一条记录,继续循环
|
|
|
}
|
|
|
c.close(); // 关闭Cursor
|
|
|
}
|
|
|
}
|
|
|
} |