|
|
@ -16,6 +16,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 导入Android系统服务相关
|
|
|
|
import android.app.AlarmManager;
|
|
|
|
import android.app.AlarmManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
@ -24,42 +25,59 @@ import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 导入项目数据模型和常量
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 闹钟初始化广播接收器 - 用于在系统启动或时间变更时重新设置所有未触发的便签提醒
|
|
|
|
|
|
|
|
* 继承自BroadcastReceiver,监听系统广播事件
|
|
|
|
|
|
|
|
*/
|
|
|
|
public class AlarmInitReceiver extends BroadcastReceiver {
|
|
|
|
public class AlarmInitReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 数据库查询投影列(需要查询的字段)
|
|
|
|
private static final String [] PROJECTION = new String [] {
|
|
|
|
private static final String [] PROJECTION = new String [] {
|
|
|
|
NoteColumns.ID,
|
|
|
|
NoteColumns.ID, // 便签ID列
|
|
|
|
NoteColumns.ALERTED_DATE
|
|
|
|
NoteColumns.ALERTED_DATE // 提醒时间列
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private static final int COLUMN_ID = 0;
|
|
|
|
// 列索引常量
|
|
|
|
private static final int COLUMN_ALERTED_DATE = 1;
|
|
|
|
private static final int COLUMN_ID = 0; // ID列的索引
|
|
|
|
|
|
|
|
private static final int COLUMN_ALERTED_DATE = 1; // 提醒时间列的索引
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
long currentDate = System.currentTimeMillis();
|
|
|
|
long currentDate = System.currentTimeMillis(); // 获取当前系统时间
|
|
|
|
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
|
|
|
PROJECTION,
|
|
|
|
// 查询内容提供者获取需要设置提醒的便签
|
|
|
|
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
|
|
|
|
Cursor c = context.getContentResolver().query(
|
|
|
|
new String[] { String.valueOf(currentDate) },
|
|
|
|
Notes.CONTENT_NOTE_URI, // 便签内容URI
|
|
|
|
null);
|
|
|
|
PROJECTION, // 需要查询的列
|
|
|
|
|
|
|
|
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE, // 筛选条件:提醒时间未过期且类型为普通便签
|
|
|
|
|
|
|
|
new String[] { String.valueOf(currentDate) }, // 当前时间作为筛选参数
|
|
|
|
|
|
|
|
null); // 排序方式(无)
|
|
|
|
|
|
|
|
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
if (c.moveToFirst()) { // 游标移动到第一条记录
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
|
|
|
|
long alertDate = c.getLong(COLUMN_ALERTED_DATE); // 获取提醒时间
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建发送给AlarmReceiver的Intent
|
|
|
|
Intent sender = new Intent(context, AlarmReceiver.class);
|
|
|
|
Intent sender = new Intent(context, AlarmReceiver.class);
|
|
|
|
|
|
|
|
// 设置数据URI:Note内容URI + 当前便签ID
|
|
|
|
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
|
|
|
|
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建PendingIntent(延迟触发的广播)
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
|
|
|
|
AlarmManager alermManager = (AlarmManager) context
|
|
|
|
|
|
|
|
|
|
|
|
// 获取系统闹钟服务
|
|
|
|
|
|
|
|
AlarmManager alarmManager = (AlarmManager) context
|
|
|
|
.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
|
// 设置精确闹钟(RTC_WAKEUP会在指定时间唤醒设备)
|
|
|
|
} while (c.moveToNext());
|
|
|
|
alarmManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
|
|
|
|
|
} while (c.moveToNext()); // 循环处理下一条记录
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.close();
|
|
|
|
c.close(); // 关闭游标释放资源
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|