|
|
/*
|
|
|
* 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.
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
* 文件: AlarmInitReceiver.java
|
|
|
* 描述: 便签提醒初始化广播接收器
|
|
|
* 作用: 在系统启动或应用启动时,重新初始化所有未触发的便签提醒
|
|
|
* 功能:
|
|
|
* 1. 监听系统启动广播
|
|
|
* 2. 查询数据库中所有未触发的提醒
|
|
|
* 3. 为每个未触发的提醒重新设置闹钟
|
|
|
*/
|
|
|
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, // 便签ID
|
|
|
NoteColumns.ALERTED_DATE // 提醒时间
|
|
|
};
|
|
|
|
|
|
/** 便签ID列索引 */
|
|
|
private static final int COLUMN_ID = 0;
|
|
|
|
|
|
/** 提醒时间列索引 */
|
|
|
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,
|
|
|
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);
|
|
|
|
|
|
// 创建用于触发AlarmReceiver的Intent
|
|
|
Intent sender = new Intent(context, AlarmReceiver.class);
|
|
|
// 设置Intent的数据为便签的URI
|
|
|
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
|
|
|
|
|
|
// 创建PendingIntent,用于延迟触发
|
|
|
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();
|
|
|
}
|
|
|
}
|
|
|
}
|