|
|
|
@ -27,39 +27,53 @@ import android.database.Cursor;
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 闹钟初始化广播接收器
|
|
|
|
|
* 功能:在设备启动后重新设置所有未触发的便签提醒
|
|
|
|
|
*/
|
|
|
|
|
public class AlarmInitReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
|
|
// 查询数据库的列投影
|
|
|
|
|
private static final String [] PROJECTION = new String [] {
|
|
|
|
|
NoteColumns.ID,
|
|
|
|
|
NoteColumns.ALERTED_DATE
|
|
|
|
|
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; // 提醒日期列索引
|
|
|
|
|
|
|
|
|
|
@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) },
|
|
|
|
|
new String[] { String.valueOf(currentDate) }, // 参数:当前时间
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
if (c != null) {
|
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
// 获取提醒时间
|
|
|
|
|
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
|
|
|
|
|
// 创建闹钟触发时要发送的Intent
|
|
|
|
|
Intent sender = new Intent(context, AlarmReceiver.class);
|
|
|
|
|
// 设置便签URI
|
|
|
|
|
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);
|
|
|
|
|
// 设置闹钟
|
|
|
|
|
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
|
|
|
|
|
} while (c.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|