|
|
|
@ -0,0 +1,75 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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类,表示Notes表的列
|
|
|
|
|
|
|
|
|
|
public class AlarmInitReceiver extends BroadcastReceiver { // 定义一个继承自BroadcastReceiver的类
|
|
|
|
|
|
|
|
|
|
// 定义查询需要的列
|
|
|
|
|
private static final String [] PROJECTION = new String [] {
|
|
|
|
|
NoteColumns.ID, // 获取笔记的ID列
|
|
|
|
|
NoteColumns.ALERTED_DATE // 获取提醒时间列
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 列索引常量
|
|
|
|
|
private static final int COLUMN_ID = 0; // 第0列是ID列
|
|
|
|
|
private static final int COLUMN_ALERTED_DATE = 1; // 第1列是提醒时间列
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) { // 重写onReceive方法,接收广播
|
|
|
|
|
long currentDate = System.currentTimeMillis(); // 获取当前的系统时间(以毫秒为单位)
|
|
|
|
|
|
|
|
|
|
// 查询数据库,获取需要提醒的笔记,且提醒时间大于当前时间
|
|
|
|
|
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI, // 查询URI为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,用于发送广播
|
|
|
|
|
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID))); // 设置Intent的数据(笔记的ID)
|
|
|
|
|
|
|
|
|
|
// 创建PendingIntent,表示在指定时间执行AlarmReceiver广播
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
|
|
|
|
|
|
|
|
|
|
// 获取系统的AlarmManager服务
|
|
|
|
|
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
|
|
|
|
|
// 设置闹钟,使用RTC_WAKEUP模式,当到达指定的提醒时间时唤醒设备并触发PendingIntent
|
|
|
|
|
alarmManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
|
|
} while (c.moveToNext()); // 如果查询结果中还有下一条记录,则继续处理
|
|
|
|
|
}
|
|
|
|
|
c.close(); // 关闭Cursor
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|