|
|
|
@ -14,52 +14,67 @@
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
|
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;
|
|
|
|
|
import android.app.AlarmManager; // 导入AlarmManager类,用于管理闹钟
|
|
|
|
|
import android.app.PendingIntent; // 导入PendingIntent类,用于延迟执行的Intent
|
|
|
|
|
import android.content.BroadcastReceiver; // 导入BroadcastReceiver类,用于接收广播
|
|
|
|
|
import android.content.ContentUris; // 导入ContentUris类,用于处理URI
|
|
|
|
|
import android.content.Context; // 导入Context类,用于访问应用程序环境的全局信息
|
|
|
|
|
import android.content.Intent; // 导入Intent类,用于启动活动或服务
|
|
|
|
|
import android.database.Cursor; // 导入Cursor类,用于遍历查询结果
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.data.Notes; // 导入Notes类,用于访问笔记数据
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns; // 导入NoteColumns类,用于访问笔记列
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AlarmInitReceiver类用于接收系统广播并初始化闹钟。
|
|
|
|
|
*/
|
|
|
|
|
public class AlarmInitReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
private static final String [] PROJECTION = new String [] {
|
|
|
|
|
NoteColumns.ID,
|
|
|
|
|
NoteColumns.ALERTED_DATE
|
|
|
|
|
// 定义查询所需的列
|
|
|
|
|
private static final String[] PROJECTION = new String[]{
|
|
|
|
|
NoteColumns.ID, // 笔记ID
|
|
|
|
|
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; // 提醒日期列索引
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当接收到广播时调用的方法。
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
* @param intent 接收到的意图
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
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,
|
|
|
|
|
new String[] { String.valueOf(currentDate) },
|
|
|
|
|
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);
|
|
|
|
|
long alertDate = c.getLong(COLUMN_ALERTED_DATE); // 获取提醒日期
|
|
|
|
|
Intent sender = new Intent(context, AlarmReceiver.class); // 创建Intent,用于触发AlarmReceiver
|
|
|
|
|
// 设置数据URI,包含笔记ID
|
|
|
|
|
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());
|
|
|
|
|
// 获取AlarmManager服务
|
|
|
|
|
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
// 设置闹钟
|
|
|
|
|
alarmManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
|
|
} while (c.moveToNext()); // 继续遍历游标
|
|
|
|
|
}
|
|
|
|
|
c.close();
|
|
|
|
|
c.close(); // 关闭游标
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|