You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zuoye/src/net/micode/notes/ui/AlarmInitReceiver.java

80 lines
3.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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 {
// 定义查询笔记的列,只查询 ID 和 ALERTED_DATE
private static final String [] PROJECTION = new String [] {
NoteColumns.ID, // 笔记 ID
NoteColumns.ALERTED_DATE // 警报时间(即提醒时间)
};
// 对应查询结果的列索引
private static final int COLUMN_ID = 0; // 笔记 ID 列索引
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用于广播 AlarmReceiver
Intent sender = new Intent(context, AlarmReceiver.class);
// 设置 Intent 的数据为当前笔记的 URI
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建一个 PendingIntent用于设置警报
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 获取 AlarmManager 实例
AlarmManager alermManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// 设置警报:在指定时间触发广播
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext()); // 如果还有下一条记录,继续处理
}
// 关闭数据库游标
c.close();
}
}
}