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.
2Q1/ui/AlarmInitReceiver.java

88 lines
5.4 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.
*/
// 包声明表明该类所在的包名为net.micode.notes.ui
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;
// AlarmInitReceiver类继承自BroadcastReceiver用于接收广播并处理与闹钟初始化相关的逻辑比如根据条件设置闹钟提醒等操作
public class AlarmInitReceiver extends BroadcastReceiver {
// 定义一个字符串数组用于指定从数据库查询时需要获取的列名这里获取笔记的ID和提醒日期这两列信息
private static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.ALERTED_DATE
};
// 定义常量用于表示查询结果中笔记ID所在列的索引方便后续从Cursor中获取对应数据值为0表示第一列
private static final int COLUMN_ID = 0;
// 定义常量用于表示查询结果中提醒日期所在列的索引方便后续从Cursor中获取对应数据值为1表示第二列
private static final int COLUMN_ALERTED_DATE = 1;
// 重写BroadcastReceiver的onReceive方法当接收到广播时会被调用在此方法中实现具体的业务逻辑
@Override
public void onReceive(Context context, Intent intent) {
// 获取当前的系统时间(以毫秒为单位),用于后续与笔记的提醒日期进行比较,判断哪些笔记需要设置闹钟提醒
long currentDate = System.currentTimeMillis();
// 通过ContentResolver发起一个数据库查询操作从指定的笔记内容URINotes.CONTENT_NOTE_URI中查询数据
// 查询的列由PROJECTION指定查询条件是提醒日期大于当前日期并且笔记类型为普通笔记Notes.TYPE_NOTE
// 条件中的参数通过后面的字符串数组传入当前时间的值
// 最后一个参数null表示查询不需要排序等额外设置
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);
// 判断查询结果的Cursor是否为空如果不为空表示有满足条件的数据进行后续操作
if (c!= null) {
// 将Cursor的指针移动到第一条数据位置如果有数据则返回true可以开始遍历数据
if (c.moveToFirst()) {
// 使用do-while循环来遍历查询结果集确保至少会执行一次循环体中的操作
do {
// 从Cursor中获取提醒日期这一列的数据通过之前定义的列索引COLUMN_ALERTED_DATE得到具体的提醒时间戳
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
// 创建一个Intent用于指定当闹钟触发时要启动的组件这里指定为AlarmReceiver类
Intent sender = new Intent(context, AlarmReceiver.class);
// 给Intent设置数据将笔记的ID附加到对应的内容URI上这样可以传递具体是哪个笔记的闹钟触发了
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建一个PendingIntent用于包装前面创建的Intent以便交给AlarmManager使用
// 这里的请求码设置为0表示没有特定的请求标识并且使用的是广播类型的PendingIntent
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 获取系统的AlarmManager服务用于设置闹钟相关的操作
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
// 通过AlarmManager设置一个闹钟使用RTC_WAKEUP模式表示在指定的绝对时间唤醒设备来触发闹钟
// 指定提醒时间为前面获取的alertDate并且关联对应的PendingIntent当时间到达时会触发对应的广播
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext()); // 判断是否还有下一条数据,如果有则继续循环执行操作,处理下一条满足条件的笔记数据
}
// 关闭Cursor释放相关的资源避免内存泄漏等问题
c.close();
}
}
}