|
|
/*
|
|
|
* 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();
|
|
|
}
|
|
|
}
|
|
|
} |