注释代码2

pq5n3hobs 9 months ago
parent d44cc9c417
commit bfafaacee1

@ -0,0 +1,96 @@
/*
* 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;
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 {
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;
@Override
public class AlarmInitReceiver extends BroadcastReceiver {
// 定义一个字符串数组用于指定从数据库查询时要获取的列名这里只选择获取笔记的ID和提醒日期这两列信息
private static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.ALERTED_DATE
};
// 定义一个常量表示在查询结果集中笔记ID列对应的索引位置方便后续从Cursor中获取对应的数据初始化为0对应上面PROJECTION数组中的第一个元素位置
private static final int COLUMN_ID = 0;
// 定义一个常量表示在查询结果集中提醒日期列对应的索引位置方便后续从Cursor中获取对应的数据初始化为1对应上面PROJECTION数组中的第二个元素位置
private static final int COLUMN_ALERTED_DATE = 1;
// 重写BroadcastReceiver类的onReceive方法当此BroadcastReceiver接收到相应广播时该方法会被调用用于处理接收到广播后的具体逻辑
@Override
public void onReceive(Context context, Intent intent) {
// 获取当前系统的时间,以毫秒为单位,通常用于后续与数据库中存储的提醒时间进行比较等操作
long currentDate = System.currentTimeMillis();
// 通过上下文对象的内容解析器用于和数据库等数据源交互发起一个查询操作从Notes.CONTENT_NOTE_URI指定的数据源可能是笔记相关的数据库表对应的内容Uri中查询数据
// 查询的列由PROJECTION数组指定查询条件是提醒日期大于当前日期并且笔记类型为Notes.TYPE_NOTE这里的具体类型定义应该在Notes类中有定义
// 查询条件的参数通过后面的new String[] { String.valueOf(currentDate) }提供最后的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) {
// 将游标移动到查询结果集的第一条数据位置如果有数据则返回true否则返回false这里确保有数据才进行后续循环遍历操作
if (c.moveToFirst()) {
do {
// 从游标当前位置获取提醒日期这一列的数据通过之前定义的COLUMN_ALERTED_DATE索引获取并赋值给alertDate变量数据类型为long代表时间戳
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
// 创建一个新的Intent对象用于指定当闹钟触发时要启动的组件这里指定启动AlarmReceiver类对应的组件通常在后续会配合AlarmManager使用
Intent sender = new Intent(context, AlarmReceiver.class);
// 通过ContentUris工具类将当前笔记的ID从游标中获取通过COLUMN_ID索引附加到Notes.CONTENT_NOTE_URI上构建一个完整的、指向特定笔记记录的Uri
// 并设置到Intent的Data属性中方便接收方如AlarmReceiver获取是哪个笔记对应的闹钟触发了
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建一个PendingIntent对象它基于前面创建的Intent用于在特定条件下由AlarmManager等触发时启动对应的组件
// 参数0表示请求码此处暂未用到特定请求码场景所以用0sender是要执行的Intent最后的0表示一些标志位此处用默认的0
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 获取系统的AlarmManager服务用于后续设置闹钟相关的操作通过上下文对象的getSystemService方法并传入Context.ALARM_SERVICE来获取
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
// 使用AlarmManager设置一个闹钟指定闹钟类型为RTC_WAKEUP表示在指定的绝对时间触发并且如果设备处于睡眠状态会唤醒设备
// alertDate是触发时间之前从游标中获取的提醒日期时间戳pendingIntent是当闹钟触发时要执行的操作启动对应的组件等
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
}
// 关闭游标,释放相关资源,避免内存泄漏等问题,在使用完游标后应该及时关闭
c.close();
}
}
}
Loading…
Cancel
Save