|
|
|
|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
*
|
|
|
* 版权声明:本文件由MiCode开源社区开发,遵循Apache License, Version 2.0协议;
|
|
|
* 您仅在遵守协议的前提下使用本文件,完整协议可通过以下链接获取:
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
* 注:未书面明确要求时,本软件按"原样"提供,不附带任何明示或暗示的保证。
|
|
|
*/
|
|
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
|
|
import android.app.AlarmManager;
|
|
|
import android.app.PendingIntent;
|
|
|
import android.content.BroadcastReceiver;
|
|
|
import android.content.ContentUris;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.database.Cursor;
|
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
|
/**
|
|
|
* 闹钟初始化广播接收器类
|
|
|
* 功能:接收系统广播后,从数据库中读取未触发的笔记提醒时间,初始化对应的闹钟设置
|
|
|
*/
|
|
|
public class AlarmInitReceiver extends BroadcastReceiver {
|
|
|
|
|
|
// 数据库查询投影:指定查询的字段为笔记ID和提醒时间
|
|
|
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; // 提醒时间索引
|
|
|
|
|
|
/**
|
|
|
* 接收广播时的回调方法(核心逻辑:初始化未触发的笔记提醒闹钟)
|
|
|
* @param context 上下文环境
|
|
|
* @param intent 接收到的广播意图(通常由系统或应用触发)
|
|
|
*/
|
|
|
@Override
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
long currentDate = System.currentTimeMillis(); // 获取当前系统时间(毫秒级时间戳)
|
|
|
|
|
|
// 查询数据库:获取所有未触发的普通笔记(提醒时间 > 当前时间且类型为笔记)
|
|
|
Cursor c = context.getContentResolver().query(
|
|
|
Notes.CONTENT_NOTE_URI, // 查询的Uri(笔记表)
|
|
|
PROJECTION, // 需要查询的字段(ID和提醒时间)
|
|
|
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
|
|
|
// 查询条件:提醒时间 > 当前时间 且 类型为普通笔记(TYPE_NOTE)
|
|
|
new String[]{String.valueOf(currentDate)}, // 查询参数(当前时间)
|
|
|
null // 排序方式(默认)
|
|
|
);
|
|
|
|
|
|
if (c != null) {
|
|
|
if (c.moveToFirst()) { // 移动到查询结果的第一条记录
|
|
|
do {
|
|
|
// 提取当前笔记的提醒时间和ID
|
|
|
long alertDate = c.getLong(COLUMN_ALERTED_DATE); // 提醒时间戳
|
|
|
long noteId = c.getLong(COLUMN_ID); // 笔记ID
|
|
|
|
|
|
// 创建触发闹钟提醒的Intent(指向AlarmReceiver广播接收器)
|
|
|
Intent sender = new Intent(context, AlarmReceiver.class);
|
|
|
// 设置Intent的数据为当前笔记的Uri(格式:content://.../notes/[noteId])
|
|
|
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId));
|
|
|
|
|
|
// 创建PendingIntent(用于在闹钟触发时发送广播)
|
|
|
// 参数说明:context-上下文;requestCode-请求码(0表示不区分不同请求);sender-目标Intent;flags-标志位(0为默认)
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
|
|
|
|
|
|
// 获取系统闹钟服务实例
|
|
|
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
// 设置闹钟:使用RTC_WAKEUP模式(唤醒设备),触发时间为alertDate,绑定PendingIntent
|
|
|
alarmManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
} while (c.moveToNext()); // 遍历所有符合条件的笔记记录
|
|
|
}
|
|
|
c.close(); // 关闭游标释放资源
|
|
|
}
|
|
|
}
|
|
|
} |