|
|
|
|
@ -28,38 +28,65 @@ 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,
|
|
|
|
|
NoteColumns.ALERTED_DATE
|
|
|
|
|
NoteColumns.ID, // 便签ID
|
|
|
|
|
NoteColumns.ALERTED_DATE // 提醒日期时间
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static final int COLUMN_ID = 0;
|
|
|
|
|
private static final int COLUMN_ALERTED_DATE = 1;
|
|
|
|
|
// 投影列索引常量,便于代码引用
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
// 查询所有提醒时间在当前时间之后的普通便签
|
|
|
|
|
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);
|
|
|
|
|
new String[] { String.valueOf(currentDate) }, // 查询参数
|
|
|
|
|
null); // 不指定排序
|
|
|
|
|
|
|
|
|
|
// 处理查询结果
|
|
|
|
|
if (c != null) {
|
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
|
// 遍历所有符合条件的便签
|
|
|
|
|
do {
|
|
|
|
|
// 获取便签的提醒时间
|
|
|
|
|
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
|
|
|
|
|
|
|
|
|
|
// 创建一个将在提醒时间触发的广播Intent
|
|
|
|
|
Intent sender = new Intent(context, AlarmReceiver.class);
|
|
|
|
|
// 设置Intent的数据为便签的URI,以便在广播接收器中识别具体便签
|
|
|
|
|
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
|
|
|
|
|
|
|
|
|
|
// 创建一个PendingIntent,用于在指定时间触发广播
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
|
|
|
|
|
|
|
|
|
|
// 获取系统闹钟服务
|
|
|
|
|
AlarmManager alermManager = (AlarmManager) context
|
|
|
|
|
.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
|
|
|
|
|
// 设置一个精确的闹钟,当到达提醒时间时唤醒设备并触发广播
|
|
|
|
|
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
|
|
} while (c.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
// 关闭Cursor,释放资源
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|