|
|
/*
|
|
|
* 版权所有 (c) 2010-2011, The MiCode 开源社区 (www.micode.net)
|
|
|
*
|
|
|
* 根据 Apache 许可证 2.0 版(“许可证”)授权;
|
|
|
* 除非遵守许可证,否则不得使用此文件。
|
|
|
* 您可以在以下网址获得许可证的副本:
|
|
|
*
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
*
|
|
|
* 除非适用法律要求或书面同意,否则根据许可证分发的软件
|
|
|
* 是按“原样”分发的,不附带任何明示或暗示的保证或条件。
|
|
|
* 请参阅许可证,了解许可证下的权利和限制的具体语言。
|
|
|
*/
|
|
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
|
|
// 导入 Android 相关类和接口
|
|
|
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;
|
|
|
|
|
|
// 定义 AlarmInitReceiver 类,继承自 BroadcastReceiver
|
|
|
public class AlarmInitReceiver extends BroadcastReceiver {
|
|
|
|
|
|
// 定义查询数据库时使用的列数组
|
|
|
private static final String [] PROJECTION = new String [] {
|
|
|
NoteColumns.ID,
|
|
|
NoteColumns.ALERTED_DATE
|
|
|
};
|
|
|
|
|
|
// 定义列索引常量
|
|
|
private static final int COLUMN_ID = 0;
|
|
|
private static final int COLUMN_ALERTED_DATE = 1;
|
|
|
|
|
|
// onReceive 方法是 BroadcastReceiver 接收到广播时回调的方法
|
|
|
@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);
|
|
|
|
|
|
// 如果查询结果不为空
|
|
|
if (c != null) {
|
|
|
// 如果查询结果集有数据
|
|
|
if (c.moveToFirst()) {
|
|
|
// 遍历查询结果集
|
|
|
do {
|
|
|
// 获取提醒日期
|
|
|
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
|
|
|
// 创建一个意图,用于触发 AlarmReceiver
|
|
|
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);
|
|
|
// 设置闹钟,使用 RTC_WAKEUP 模式
|
|
|
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
} while (c.moveToNext());
|
|
|
}
|
|
|
// 关闭游标
|
|
|
c.close();
|
|
|
}
|
|
|
}
|
|
|
} |