ADD file via upload

lxy
pz96pqh8i 9 months ago
parent 0c83deb731
commit 3d5939c01d

@ -0,0 +1,152 @@
/*
* 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 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);
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
}
c.close();
}
}
}
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类继承自BroadcastReceiver用于接收系统广播并处理与闹钟初始化相关的操作
// 其主要功能是从数据库中查询满足条件的便签记录,并为这些便签设置对应的闹钟提醒
public class AlarmInitReceiver extends BroadcastReceiver {
// 定义一个字符串数组,用于指定从数据库中查询时需要获取的列名
// 这里获取的是便签的唯一标识符ID和提醒日期ALERTED_DATE两列
private static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.ALERTED_DATE
};
// 定义常量表示查询结果中ID列对应的索引位置方便后续从Cursor中获取数据
private static final int COLUMN_ID = 0;
// 定义常量表示查询结果中ALERTED_DATE列对应的索引位置方便后续从Cursor中获取数据
private static final int COLUMN_ALERTED_DATE = 1;
// onReceive方法是BroadcastReceiver的核心方法当接收到相应广播时会被调用
@Override
public void onReceive(Context context, Intent intent) {
// 获取当前系统的时间戳(以毫秒为单位),用于后续在数据库查询中作为比较条件,筛选出需要设置闹钟提醒的便签
long currentDate = System.currentTimeMillis();
// 通过ContentResolver查询数据库获取满足条件的便签记录信息
// 参数说明:
// 1. Notes.CONTENT_NOTE_URI指定查询的内容提供器的Uri指向便签相关的数据表
// 2. PROJECTION前面定义的要查询的列名数组即获取便签的ID和提醒日期两列数据
// 3. NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE查询条件筛选出提醒日期大于当前日期且类型为普通便签假设Notes.TYPE_NOTE代表普通便签类型的记录
// 4. new String[] { String.valueOf(currentDate) }:为查询条件中的占位符(?)提供实际的值,即当前日期
// 5. 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-while循环遍历查询结果集处理每条满足条件的便签记录
do {
// 从Cursor中获取提醒日期这一列的值以长整型表示对应之前定义的COLUMN_ALERTED_DATE索引位置
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
// 创建一个Intent对象用于启动AlarmReceiver可能是处理闹钟提醒触发逻辑的广播接收器
Intent sender = new Intent(context, AlarmReceiver.class);
// 为Intent设置数据部分通过ContentUris工具类将便签的唯一标识符ID附加到便签内容提供器的Uri上
// 这样接收方可以根据这个Uri获取到具体是哪个便签的闹钟提醒触发了
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建一个PendingIntent对象用于包装前面创建的Intent使得它可以在稍后被触发例如作为闹钟触发时启动的目标意图
// 参数说明:
// 1. context当前的应用上下文环境
// 2. 0请求码这里暂时设置为0一般如果有多个不同用途的PendingIntent可以设置不同的请求码来区分
// 3. sender要包装的Intent对象即前面创建的启动AlarmReceiver的Intent
// 4. 0标志位这里暂时设置为0根据不同需求可以设置不同的标志来控制PendingIntent的行为
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 获取系统的AlarmManager服务AlarmManager用于管理和设置闹钟相关的操作
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
// 使用AlarmManager设置闹钟提醒具体参数含义如下
// 1. AlarmManager.RTC_WAKEUP闹钟类型表示在指定的时间alertDate唤醒设备并触发闹钟如果设备处于睡眠状态
// 2. alertDate设置闹钟触发的时间即从数据库中获取的该便签的提醒日期对应的时间戳以毫秒为单位
// 3. pendingIntent前面创建的PendingIntent对象当闹钟触发时会启动对应的Intent这里是启动AlarmReceiver
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
}
// 关闭游标,释放相关的系统资源,避免内存泄漏等问题,查询操作完成后需要及时关闭游标
c.close();
}
}
}
Loading…
Cancel
Save