From 873438d8f360ba7034f97691a0a1c6033fe2a561 Mon Sep 17 00:00:00 2001 From: LZ <2929718516@qq.com> Date: Wed, 25 Dec 2024 19:01:06 +0800 Subject: [PATCH] =?UTF-8?q?AlarmInitReceiver=E6=98=AF=E4=B8=80=E4=B8=AABro?= =?UTF-8?q?adcastReceiver=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E9=97=B9=E9=92=9F=E3=80=82=E5=BD=93=E6=8E=A5=E6=94=B6?= =?UTF-8?q?=E5=88=B0=E5=B9=BF=E6=92=AD=E6=97=B6=EF=BC=8C=E5=AE=83=E4=BC=9A?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=95=B0=E6=8D=AE=E5=BA=93=E4=B8=AD=E6=89=80?= =?UTF-8?q?=E6=9C=89=E9=9C=80=E8=A6=81=E6=8F=90=E9=86=92=EF=BC=88=E6=8F=90?= =?UTF-8?q?=E9=86=92=E6=97=A5=E6=9C=9F=E5=A4=A7=E4=BA=8E=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E6=97=B6=E9=97=B4=EF=BC=89=E4=B8=94=E7=B1=BB=E5=9E=8B=E4=B8=BA?= =?UTF-8?q?=E6=99=AE=E9=80=9A=E7=AC=94=E8=AE=B0=E7=9A=84=E6=9D=A1=E7=9B=AE?= =?UTF-8?q?=E3=80=82=E5=AF=B9=E4=BA=8E=E6=AF=8F=E4=B8=AA=E7=AC=A6=E5=90=88?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E7=9A=84=E7=AC=94=E8=AE=B0=EF=BC=8C=E5=AE=83?= =?UTF-8?q?=E4=BC=9A=E5=88=9B=E5=BB=BA=E4=B8=80=E4=B8=AAPendingIntent?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E4=BD=BF=E7=94=A8AlarmManager=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=B8=80=E4=B8=AA=E9=97=B9=E9=92=9F=EF=BC=8C=E8=AF=A5?= =?UTF-8?q?=E9=97=B9=E9=92=9F=E5=9C=A8=E7=AC=94=E8=AE=B0=E7=9A=84=E6=8F=90?= =?UTF-8?q?=E9=86=92=E6=97=A5=E6=9C=9F=E5=88=B0=E8=BE=BE=E6=97=B6=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E3=80=82=E5=BD=93=E9=97=B9=E9=92=9F=E8=A7=A6=E5=8F=91?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E4=BC=9A=E5=B9=BF=E6=92=AD=E4=B8=80=E4=B8=AA?= =?UTF-8?q?Intent=EF=BC=8C=E8=AF=A5Intent=E8=A2=ABAlarmReceiver=EF=BC=88?= =?UTF-8?q?=E6=9C=AA=E5=9C=A8=E4=BB=A3=E7=A0=81=E4=B8=AD=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=EF=BC=8C=E4=BD=86=E5=81=87=E8=AE=BE=E5=AD=98=E5=9C=A8=EF=BC=89?= =?UTF-8?q?=E6=8E=A5=E6=94=B6=E5=B9=B6=E5=A4=84=E7=90=86=EF=BC=8C=E9=80=9A?= =?UTF-8?q?=E5=B8=B8=E7=94=A8=E4=BA=8E=E6=98=BE=E7=A4=BA=E6=8F=90=E9=86=92?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AlarmInitReceiver.java | 76 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 AlarmInitReceiver.java diff --git a/AlarmInitReceiver.java b/AlarmInitReceiver.java new file mode 100644 index 0000000..a634deb --- /dev/null +++ b/AlarmInitReceiver.java @@ -0,0 +1,76 @@ +/* + * 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; +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; + +// 定义一个继承自BroadcastReceiver的类,用于初始化闹钟 +public class AlarmInitReceiver extends BroadcastReceiver { + + // 定义查询数据库时需要的列 + private static final String [] PROJECTION = new String [] { + NoteColumns.ID, // 笔记的ID + NoteColumns.ALERTED_DATE // 笔记的提醒日期 + }; + + // 定义列索引,用于从Cursor中获取数据 + 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,用于在提醒时间到达时广播 + Intent sender = new Intent(context, AlarmReceiver.class); + // 设置Intent的URI,包含笔记的ID + sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID))); + // 创建一个PendingIntent,用于AlarmManager在提醒时间到达时启动 + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0); + // 获取AlarmManager服务 + AlarmManager alermManager = (AlarmManager) context + .getSystemService(Context.ALARM_SERVICE); + // 设置闹钟,在提醒时间到达时广播Intent + alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent); + } while (c.moveToNext()); // 处理查询结果中的下一条记录 + } + c.close(); // 关闭Cursor + } + } +} \ No newline at end of file