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.
gitProject1/doc/向金成——精读代码/ui/AlarmInitReceiver.java

81 lines
4.1 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.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;
public class AlarmInitReceiver extends BroadcastReceiver {
private static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.ALERTED_DATE
};//定义了一个字符串数组
private static final int COLUMN_ID = 0;//一栏的id
private static final int COLUMN_ALERTED_DATE = 1;//一栏的闹钟响的日期
@Override
public void onReceive(Context context, Intent intent) {/*
每个receiver类都必须重写父类Receiver的onReceive方法对接收到的广播进行处理
*/
long currentDate = System.currentTimeMillis();//获取当前时间,毫秒为单位
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,//URI代表数据库的中的某个表
PROJECTION,//要查询的列
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,/*
where 后面的条件为占位符selectionArgs参数替代占位符
*/
new String[] { String.valueOf(currentDate) },//替代占位符的
null);//order by 排序当前null默认升序
/* 内容解析器提供的query查询方法会将参数转化为一条select语句用于在数据库中进行操作返回的结果相当于游标的缓冲区
cursor为指向每行的游标指针
*/
if (c != null) {//查询结果成功
if (c.moveToFirst()) {//移动到第一行
do {
long alertDate = c.getLong(COLUMN_ALERTED_DATE);//将COLUMN_ALERTED_DATE列的值转化为long赋值给变量
Intent sender = new Intent(context, AlarmReceiver.class);//向AlarmReceiver类传递intent对象
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));/*
设置sender对象的data的URI向给定的URI路径末尾添加一个id,有id后的URI代表那个id指向的资源
*/
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);/*
调用PendingIntent.getBroadcast方法以获得一个PendingIntent对象激发该对象时该对象会启动一个它能启动组件
只能是activityservicebroadcast这3个组件当前是创建了一个激发时能启动broadcast的对象广播传递的intent
是sender
*/
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);//通过系统服务来获得当前的闹钟管理器
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);/*
设置闹钟,闹钟类型是使用现实时间的闹钟,会唤醒系统进行提示 响,设置了响的时间,响时会传递的消息
*/
} while (c.moveToNext());//c指向下一行
}
c.close();//关闭游标
}
}
}