/* * 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 /** * 报警初始化接收器 * * 该类用于接收报警初始化的广播,并根据数据库中的提醒时间设置报警 * 它查询所有提醒时间大于当前日期的笔记,并为它们设置报警 */ 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 { // 定义查询投影,包含笔记的ID和提醒时间 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; /** * 当接收到广播时调用 * * @param context 上下文,用于访问应用程序特定的资源和类 * @param intent 接收到的广播意图 */ @Override public void onReceive(Context context, Intent intent) { // 获取当前时间 long currentDate = System.currentTimeMillis(); // 查询所有提醒时间大于当前时间的笔记 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 sender = new Intent(context, AlarmReceiver.class); // 为意图设置数据,包含笔记的ID sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID))); // 创建待处理的意图 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0); // 获取报警管理器 AlarmManager alermManager = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); // 设置报警 alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent); } while (c.moveToNext()); } // 关闭游标 c.close(); } } } 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 void onReceive(Context context, Intent intent) { long currentDate = System.currentTimeMillis(); 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 sender = new Intent(context, AlarmReceiver.class); sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID))); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0); AlarmManager alermManager = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent); } while (c.moveToNext()); } c.close(); } } }