|
|
|
@ -28,38 +28,50 @@ import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//AlarmInitReceiver 类继承自BroadcastReceiver, 广播接收者用于获取系统启动或其他特定事件的通知
|
|
|
|
|
public class AlarmInitReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
//数据库查询的列名数组,只查询ID和提醒日期
|
|
|
|
|
private static final String [] PROJECTION = new String [] {
|
|
|
|
|
NoteColumns.ID,
|
|
|
|
|
NoteColumns.ALERTED_DATE
|
|
|
|
|
NoteColumns.ID, //便签的ID
|
|
|
|
|
NoteColumns.ALERTED_DATE //便签的提醒日期
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//定义两个静态常量,代表数据表列索引ID和提醒日期
|
|
|
|
|
private static final int COLUMN_ID = 0;
|
|
|
|
|
private static final int COLUMN_ALERTED_DATE = 1;
|
|
|
|
|
|
|
|
|
|
//当广播被接收时执行此方法
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
//获取系统的时间
|
|
|
|
|
long currentDate = System.currentTimeMillis();
|
|
|
|
|
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
//通过内容解析器查询便签,寻找提醒时间大于当前时间且类型为TYPE_NOTE的便签
|
|
|
|
|
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI, // CONTENT_NOTE_URI是访问便签数据的URI
|
|
|
|
|
PROJECTION,
|
|
|
|
|
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
|
|
|
|
|
new String[] { String.valueOf(currentDate) },
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
//遍历查询出的结果
|
|
|
|
|
if (c != null) {
|
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
|
if (c.moveToFirst()) { //移动游标到第一条记录
|
|
|
|
|
do {
|
|
|
|
|
//读取提醒日期时间
|
|
|
|
|
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
|
|
|
|
|
//创建指向AlarmReceiver的Intent
|
|
|
|
|
Intent sender = new Intent(context, AlarmReceiver.class);
|
|
|
|
|
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
|
|
|
|
|
//创建一个即将执行的PendingIntent
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
|
|
|
|
|
//获取系统AlarmManager
|
|
|
|
|
AlarmManager alermManager = (AlarmManager) context
|
|
|
|
|
.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
//使用AlarmManager设置一个闹钟,当系统时间达到提醒日期时触发广播
|
|
|
|
|
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
|
|
} while (c.moveToNext());
|
|
|
|
|
} while (c.moveToNext()); //移动到下一条记录
|
|
|
|
|
}
|
|
|
|
|
c.close();
|
|
|
|
|
c.close(); //关闭游标
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|