From bfafaacee163a79cd8d96c23178f6fa9d6f9d9ed Mon Sep 17 00:00:00 2001 From: pq5n3hobs Date: Thu, 21 Nov 2024 21:59:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E9=87=8A=E4=BB=A3=E7=A0=812?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AlarmInitReceiver.java | 96 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 AlarmInitReceiver.java diff --git a/AlarmInitReceiver.java b/AlarmInitReceiver.java new file mode 100644 index 0000000..fb1636d --- /dev/null +++ b/AlarmInitReceiver.java @@ -0,0 +1,96 @@ +/* + * 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 { + + private static final String [] PROJECTION = new String [] { + NoteColumns.ID, + NoteColumns.ALERTED_DATE + }; + + private static final int COLUMN_ID = 0; + private static final int COLUMN_ALERTED_DATE = 1; + + @Override + public class AlarmInitReceiver extends BroadcastReceiver { + + // 定义一个字符串数组,用于指定从数据库查询时要获取的列名,这里只选择获取笔记的ID和提醒日期这两列信息 + private static final String [] PROJECTION = new String [] { + NoteColumns.ID, + NoteColumns.ALERTED_DATE + }; + + // 定义一个常量,表示在查询结果集中笔记ID列对应的索引位置,方便后续从Cursor中获取对应的数据,初始化为0,对应上面PROJECTION数组中的第一个元素位置 + private static final int COLUMN_ID = 0; + // 定义一个常量,表示在查询结果集中提醒日期列对应的索引位置,方便后续从Cursor中获取对应的数据,初始化为1,对应上面PROJECTION数组中的第二个元素位置 + private static final int COLUMN_ALERTED_DATE = 1; + + // 重写BroadcastReceiver类的onReceive方法,当此BroadcastReceiver接收到相应广播时,该方法会被调用,用于处理接收到广播后的具体逻辑 + @Override + public void onReceive(Context context, Intent intent) { + // 获取当前系统的时间,以毫秒为单位,通常用于后续与数据库中存储的提醒时间进行比较等操作 + long currentDate = System.currentTimeMillis(); + // 通过上下文对象的内容解析器(用于和数据库等数据源交互)发起一个查询操作,从Notes.CONTENT_NOTE_URI指定的数据源(可能是笔记相关的数据库表对应的内容Uri)中查询数据 + // 查询的列由PROJECTION数组指定,查询条件是提醒日期大于当前日期并且笔记类型为Notes.TYPE_NOTE(这里的具体类型定义应该在Notes类中有定义), + // 查询条件的参数通过后面的new String[] { String.valueOf(currentDate) }提供,最后的null表示不进行排序等额外操作 + 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); + + // 判断查询结果的Cursor是否为空,如果不为空,表示有符合条件的数据需要进一步处理 + if (c!= null) { + // 将游标移动到查询结果集的第一条数据位置,如果有数据则返回true,否则返回false,这里确保有数据才进行后续循环遍历操作 + if (c.moveToFirst()) { + do { + // 从游标当前位置获取提醒日期这一列的数据(通过之前定义的COLUMN_ALERTED_DATE索引获取),并赋值给alertDate变量,数据类型为long,代表时间戳 + long alertDate = c.getLong(COLUMN_ALERTED_DATE); + // 创建一个新的Intent对象,用于指定当闹钟触发时要启动的组件,这里指定启动AlarmReceiver类对应的组件,通常在后续会配合AlarmManager使用 + Intent sender = new Intent(context, AlarmReceiver.class); + // 通过ContentUris工具类,将当前笔记的ID(从游标中获取,通过COLUMN_ID索引)附加到Notes.CONTENT_NOTE_URI上,构建一个完整的、指向特定笔记记录的Uri, + // 并设置到Intent的Data属性中,方便接收方(如AlarmReceiver)获取是哪个笔记对应的闹钟触发了 + sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID))); + // 创建一个PendingIntent对象,它基于前面创建的Intent,用于在特定条件下(由AlarmManager等触发时)启动对应的组件, + // 参数0表示请求码(此处暂未用到特定请求码场景,所以用0),sender是要执行的Intent,最后的0表示一些标志位(此处用默认的0) + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0); + // 获取系统的AlarmManager服务,用于后续设置闹钟相关的操作,通过上下文对象的getSystemService方法并传入Context.ALARM_SERVICE来获取 + AlarmManager alermManager = (AlarmManager) context + .getSystemService(Context.ALARM_SERVICE); + // 使用AlarmManager设置一个闹钟,指定闹钟类型为RTC_WAKEUP(表示在指定的绝对时间触发,并且如果设备处于睡眠状态会唤醒设备), + // alertDate是触发时间(之前从游标中获取的提醒日期时间戳),pendingIntent是当闹钟触发时要执行的操作(启动对应的组件等) + alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent); + } while (c.moveToNext()); + } + // 关闭游标,释放相关资源,避免内存泄漏等问题,在使用完游标后应该及时关闭 + c.close(); + } + } +}