/* * 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发起一个数据库查询操作,从指定的笔记内容URI(Notes.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(); } } }