You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
note/AlarmInitReceiver.java

82 lines
4.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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用于延迟执行的操作
import android.content.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类表示笔记数据表的字段
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; // 表示警报日期字段的索引
@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, // 查询条件:警报日期大于当前时间,且类型是笔记
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的Data为当前笔记的URI
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();
}
}
}