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/AlarmAlertActivity.java

99 lines
7.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.
*/
package net.micode.notes.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.tool.DataUtils;
import java.io.IOException;
public class AlarmInitReceiver extends BroadcastReceiver {
// 创建一个字符串数组这里面写的是我们从数据库查询笔记信息的时候想要获取的具体列的名字在这里只打算获取笔记的ID和笔记的提醒日期这两列内容哦
private static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.ALERTED_DATE
};
// 定义一个常量用来标记在查询结果集里笔记ID这一列对应的位置序号这里设为0就是对应着上面PROJECTION数组里第一个元素的位置方便后面准确地从结果里取出笔记ID的值
private static final int COLUMN_ID = 0;
// 同样的道理定义这个常量是为了标记在查询结果集里提醒日期这一列对应的位置序号设为1对应着PROJECTION数组里第二个元素的位置方便取提醒日期的值
private static final int COLUMN_ALERTED_DATE = 1;
// 重写了BroadcastReceiver类里的onReceive方法哦这个方法可重要啦当这个广播接收器也就是AlarmInitReceiver类的实例收到广播消息的时候就会自动调用这个方法来处理后续的事儿呢
@Override
public void onReceive(Context context, Intent intent) {
// 先获取一下当前系统的时间,这个时间是以毫秒为单位的,就像给当前时刻打了个时间戳一样,后面会拿这个时间和数据库里存的笔记提醒时间去做对比哦
long currentDate = System.currentTimeMillis();
// 通过安卓提供的内容解析器(它就像是个数据库操作的“小秘书”,能帮我们和数据库打交道)发起一个查询操作。
// 要查询的数据来源是Notes.CONTENT_NOTE_URI指定的地方一般就是笔记相关的数据库表对应的一个地址啦
// 查询哪些列呢就是前面定义的PROJECTION数组里写的那些列啦。
// 查询条件是这样的只找提醒日期大于当前日期的并且笔记类型是Notes.TYPE_NOTE这个Notes.TYPE_NOTE在Notes类里应该有具体定义表示某种特定类型的笔记的记录
// 把当前日期作为参数传给查询条件要转成字符串格式哦最后的null表示我们暂时不需要对查询结果进行排序之类的额外操作啦。
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);
// 判断一下查询出来的结果集也就是那个Cursor对象是不是为空呀如果不是空的那就说明有符合条件的数据咱们就可以接着往下处理这些数据啦
if (c!= null) {
// 把游标就是那个在结果集里“游走”的指针一样的东西移动到第一条数据的位置上如果能成功移动过去也就是有数据那就返回true然后就可以进入循环去处理每条数据啦
if (c.moveToFirst()) {
do {
// 从游标当前指向的这行数据里按照之前定义好的提醒日期列对应的索引COLUMN_ALERTED_DATE取出提醒日期这个列的值这个值是个时间戳用long类型的变量alertDate来接收它哦
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
// 创建一个新的Intent对象这个Intent就像是一个任务清单告诉安卓系统等会儿要去启动哪个组件这里指定的是要启动AlarmReceiver类对应的组件哦通常后面会配合闹钟管理的相关操作来用呢
Intent sender = new Intent(context, AlarmReceiver.class);
// 利用ContentUris这个“小助手”把当前这条笔记的ID从游标里根据COLUMN_ID索引取出来的附加到Notes.CONTENT_NOTE_URI这个基本的笔记资源地址上这样就构成了一个完整的、专门指向这条笔记记录的Uri啦
// 然后把这个完整的Uri设置到刚才创建的Intent的Data属性里这样接收这个Intent的地方比如AlarmReceiver就能知道是哪个笔记对应的闹钟触发了相关操作哦
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
// 创建一个PendingIntent对象它是基于前面创建的那个Intent来的相当于给这个Intent包了一层“外衣”让它可以在特定条件下像等会儿闹钟时间一到这种情况去启动对应的组件呢
// 这里的参数0表示请求码目前我们没什么特殊的请求码需求就先用0啦sender就是要执行的那个Intent最后的0表示一些标志位暂时用默认的0就行啦
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
// 从安卓系统里获取AlarmManager这个闹钟管理的“大管家”通过上下文对象的getSystemService方法传入Context.ALARM_SERVICE这个标识就能获取到啦后面就用它来设置闹钟相关的各种事儿哦
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
// 让AlarmManager这个“大管家”去设置一个闹钟啦这里指定闹钟的类型是RTC_WAKEUP意思就是按照绝对时间来触发闹钟就是指定的那个alertDate时间而且如果设备当时处于睡眠状态的话还会把设备唤醒哦
// alertDate就是刚才从游标里取出来的提醒时间pendingIntent就是等闹钟触发时要去执行的操作比如启动对应的组件之类的
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
}
// 用完游标了,要记得关闭它呀,就像看完书要把书签收起来一样,关闭游标可以释放相关的资源,避免占用太多内存,造成不好的影响呢
c.close();
}
}
}