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.
Dome/src1/net/micode/notes/ui/AlarmInitReceiver.java

81 lines
3.3 KiB

2 weeks ago
/*
* 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;
2 days ago
// 定义一个名为AlarmInitReceiver的类继承自BroadcastReceiver
2 weeks ago
public class AlarmInitReceiver extends BroadcastReceiver {
2 days ago
// 定义一个字符串数组包含NoteColumns中的ID和ALERTED_DATE
2 weeks ago
private static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.ALERTED_DATE
};
2 days ago
// 定义两个常量分别表示ID和ALERTED_DATE的列索引
2 weeks ago
private static final int COLUMN_ID = 0;
private static final int COLUMN_ALERTED_DATE = 1;
@Override
public void onReceive(Context context, Intent intent) {
2 days ago
// 获取当前时间
2 weeks ago
long currentDate = System.currentTimeMillis();
2 days ago
// 查询数据库,获取所有未提醒的笔记
2 weeks ago
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);
2 days ago
// 如果c不为空
2 weeks ago
if (c != null) {
2 days ago
// 如果c可以移动到第一行
2 weeks ago
if (c.moveToFirst()) {
2 days ago
// 循环遍历c
2 weeks ago
do {
2 days ago
// 获取提醒日期
2 weeks ago
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
2 days ago
// 创建一个Intent指向AlarmReceiver类
2 weeks ago
Intent sender = new Intent(context, AlarmReceiver.class);
2 days ago
// 设置Intent的数据指向Notes.CONTENT_NOTE_URI并添加c.getLong(COLUMN_ID)作为参数
2 weeks ago
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
2 days ago
// 创建一个PendingIntent用于发送广播
2 weeks ago
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
2 days ago
// 获取AlarmManager服务
2 weeks ago
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
2 days ago
// 设置闹钟在alertDate时间点唤醒设备并发送广播
2 weeks ago
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
}
2 days ago
// 关闭c
2 weeks ago
c.close();
}
}
}