You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xiaomi/ui/AlarmInitReceiver.java

89 lines
3.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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.
*/
// 这里是文件的版权声明说明代码版权归属于MiCode开源社区并在Apache License 2.0下授权。
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;
// 导入所需的Android类和接口以及应用内部的数据访问类。
public class AlarmInitReceiver extends BroadcastReceiver {
// 定义一个继承自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) {
// 实现onReceive方法这是BroadcastReceiver的核心方法用于处理接收到的广播。
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);
// 创建一个新的Intent用于触发AlarmReceiver。
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 设置Intent的数据以便AlarmReceiver知道要处理哪个笔记。
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 创建一个PendingIntent用于AlarmManager设置闹钟。
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
// 获取AlarmManager服务。
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
// 设置闹钟当到达提醒日期时AlarmManager将触发AlarmReceiver。
} while (c.moveToNext());
// 遍历查询结果,为每条记录设置闹钟。
}
c.close();
// 关闭Cursor。
}
}
}