|
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个名为AlarmInitReceiver的类,继承自BroadcastReceiver
|
|
|
|
|
public class AlarmInitReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
// 定义一个字符串数组,包含NoteColumns中的ID和ALERTED_DATE
|
|
|
|
|
private static final String [] PROJECTION = new String [] {
|
|
|
|
|
NoteColumns.ID,
|
|
|
|
|
NoteColumns.ALERTED_DATE
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 定义两个常量,分别表示ID和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) {
|
|
|
|
|
// 获取当前时间
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 如果c不为空
|
|
|
|
|
if (c != null) {
|
|
|
|
|
// 如果c可以移动到第一行
|
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
|
// 循环遍历c
|
|
|
|
|
do {
|
|
|
|
|
// 获取提醒日期
|
|
|
|
|
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
|
|
|
|
|
// 创建一个Intent,指向AlarmReceiver类
|
|
|
|
|
Intent sender = new Intent(context, AlarmReceiver.class);
|
|
|
|
|
// 设置Intent的数据,指向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);
|
|
|
|
|
// 获取AlarmManager服务
|
|
|
|
|
AlarmManager alermManager = (AlarmManager) context
|
|
|
|
|
.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
// 设置闹钟,在alertDate时间点唤醒设备,并发送广播
|
|
|
|
|
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
|
|
} while (c.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
// 关闭c
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|