/* * 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 }; // 定义列索引常量,方便后续从游标(Cursor)中获取对应列的数据,这里分别对应ID列和提醒日期列的索引 private static final int COLUMN_ID = 0; private static final int COLUMN_ALERTED_DATE = 1; // 重写BroadcastReceiver的onReceive方法,当该广播接收器接收到匹配的广播时会被调用,在此方法中执行具体的闹钟初始化逻辑 @Override public void onReceive(Context context, Intent intent) { // 获取当前的系统时间(以毫秒为单位),用于后续与笔记的提醒日期进行比较筛选 long currentDate = System.currentTimeMillis(); // 通过ContentResolver查询数据库,获取满足特定条件的笔记记录游标(Cursor) // 查询条件是提醒日期大于当前日期且笔记类型为普通笔记(Notes.TYPE_NOTE)的记录,只获取前面定义的投影列信息 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); // 如果游标不为空,说明查询到了符合条件的笔记记录 if (c!= null) { // 将游标移动到第一条记录位置,如果有记录则进入循环处理每条记录 if (c.moveToFirst()) { do { // 从游标中获取当前笔记记录的提醒日期(以毫秒为单位的时间戳) long alertDate = c.getLong(COLUMN_ALERTED_DATE); // 创建一个用于触发闹钟提醒的意图(Intent),指定其对应的广播接收类为AlarmReceiver Intent sender = new Intent(context, AlarmReceiver.class); // 设置意图的数据部分,将笔记的ID附加到对应的内容URI上,用于标识具体是哪个笔记的闹钟提醒 sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID))); // 创建一个PendingIntent,用于包装前面的sender意图,使得可以在合适的时间由系统触发该意图对应的广播 // 这里使用的请求码为0,具体的触发情况等由后续设置闹钟时关联使用 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0); // 获取系统的AlarmManager服务,用于设置闹钟相关操作 AlarmManager alermManager = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); // 通过AlarmManager设置闹钟,使用RTC_WAKEUP模式(表示在指定的绝对时间唤醒设备来触发闹钟提醒) // 指定提醒的时间为前面获取的笔记提醒日期,关联的PendingIntent就是前面创建的用于触发广播的那个 alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent); } while (c.moveToNext()); } // 关闭游标,释放相关资源,避免内存泄漏等问题 c.close(); } } }