/* * 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 {// AlarmInitReceiver类继承自BroadcastReceiver类 private static final String [] PROJECTION = new String [] {// 定义一个字符串数组PROJECTION NoteColumns.ID,// 第一个元素为NoteColumns.ID NoteColumns.ALERTED_DATE// 第二个元素为NoteColumns.ALERTED_DATE }; private static final int COLUMN_ID = 0; // 定义一个整型变量COLUMN_ID,值为0 private static final int COLUMN_ALERTED_DATE = 1;// 定义一个整型变量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);// 创建一个用于启动广播的PendingIntent AlarmManager alermManager = (AlarmManager) context// 获取AlarmManager服务 .getSystemService(Context.ALARM_SERVICE);// 设置定时器 alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);// 移动游标到下一行 } while (c.moveToNext());// 关闭游标 } c.close(); } } }