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.
pyx_gitpractice/ui/AlarmInitReceiver.java

91 lines
4.1 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;
/**
* AlarmInitReceiver 类用于在接收到广播时初始化所有未触发的提醒闹钟。
* 它会查询数据库中所有提醒日期晚于当前时间的便签,并为每个便签设置一个提醒闹钟。
*/
public class AlarmInitReceiver extends BroadcastReceiver {
// 定义查询便签时需要返回的列
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; // 便签提醒日期列的索引
/**
* 当接收到广播时调用此方法。
* 该方法会查询数据库中所有提醒日期晚于当前时间的便签,并为每个便签设置一个提醒闹钟。
*
* @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);
// 创建一个新的意图,用于触发 AlarmReceiver
Intent sender = new Intent(context, AlarmReceiver.class);
// 设置意图的数据为当前便签的 URI
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建一个 PendingIntent用于在提醒日期触发广播
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 获取系统的闹钟服务
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
// 设置闹钟,在提醒日期唤醒设备并触发 PendingIntent
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
}
// 关闭游标,释放资源
c.close();
}
}
}