|
|
/*
|
|
|
* 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;
|
|
|
|
|
|
/**
|
|
|
* 闹钟初始化接收器 - 在设备启动后重新设置所有未来的提醒闹钟
|
|
|
*
|
|
|
* 功能:
|
|
|
* 1. 监听系统启动完成广播(BOOT_COMPLETED)
|
|
|
* 2. 查询所有需要未来提醒的便签
|
|
|
* 3. 为每个便签重新设置闹钟
|
|
|
*
|
|
|
* 注意:此接收器需要在AndroidManifest.xml中注册BOOT_COMPLETED权限
|
|
|
*/
|
|
|
public class AlarmInitReceiver extends BroadcastReceiver {
|
|
|
// 查询需要的列:便签ID和提醒时间
|
|
|
private static final String [] PROJECTION = new String [] {
|
|
|
NoteColumns.ID,
|
|
|
NoteColumns.ALERTED_DATE
|
|
|
};
|
|
|
|
|
|
// 列索引常量
|
|
|
private static final int COLUMN_ID = 0; // 便签ID的列索引
|
|
|
private static final int COLUMN_ALERTED_DATE = 1; // 提醒时间的列索引
|
|
|
|
|
|
/**
|
|
|
* 接收广播时的处理逻辑(主要处理设备启动完成广播)
|
|
|
* @param context 上下文对象
|
|
|
* @param intent 接收到的广播意图
|
|
|
*/
|
|
|
@Override
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
// 获取当前系统时间(用于筛选未来提醒)
|
|
|
long currentDate = System.currentTimeMillis();
|
|
|
|
|
|
/**
|
|
|
* 查询所有符合条件的便签:
|
|
|
* - 提醒时间大于当前时间(未来提醒)
|
|
|
* - 类型为普通便签(不包括文件夹等)
|
|
|
*/
|
|
|
Cursor c = context.getContentResolver().query(
|
|
|
Notes.CONTENT_NOTE_URI, // 便签内容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);
|
|
|
// 获取便签ID
|
|
|
long noteId = c.getLong(COLUMN_ID);
|
|
|
|
|
|
// 创建闹钟触发时发送的广播意图
|
|
|
Intent sender = new Intent(context, AlarmReceiver.class);
|
|
|
// 设置便签URI作为数据标识(AlarmReceiver可通过此URI获取便签内容)
|
|
|
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId));
|
|
|
|
|
|
/**
|
|
|
* 创建PendingIntent:
|
|
|
* - 使用广播类型
|
|
|
* - requestCode为0(相同noteId会覆盖之前的闹钟)
|
|
|
* - FLAG_UPDATE_CURRENT:如果已存在则更新其extra数据
|
|
|
*/
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
|
|
context,
|
|
|
0,
|
|
|
sender,
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
// 获取系统闹钟服务
|
|
|
AlarmManager alarmManager = (AlarmManager) context
|
|
|
.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
|
/**
|
|
|
* 设置精确闹钟:
|
|
|
* - RTC_WAKEUP:在指定时间唤醒设备并触发闹钟
|
|
|
* - alertDate:提醒时间(毫秒级时间戳)
|
|
|
* - pendingIntent:触发时执行的广播
|
|
|
*/
|
|
|
if (alarmManager != null) {
|
|
|
alarmManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
}
|
|
|
} while (c.moveToNext()); // 处理下一个便签
|
|
|
}
|
|
|
// 关闭游标释放资源
|
|
|
c.close();
|
|
|
}
|
|
|
}
|
|
|
} |