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.
xiaomi-Notes/AlarmInitReceiver.java

81 lines
3.3 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;
// 导入所需的Android类
import android.app.AlarmManager; // 用于管理闹钟服务
import android.app.PendingIntent; // 用于延迟执行的意图
import android.content.BroadcastReceiver; // 广播接收器基类
import android.content.ContentUris; // 用于处理内容URI
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 {
// 定义要查询的列
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 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服务
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// 设置闹钟
alarmManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
// 遍历结果集
}
c.close();
// 关闭Cursor
}
}
}