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.
XiaoMiNote/AlarmInitReceiver.java.txt

52 lines
2.2 KiB

2 years ago
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 years ago
//以上是引入依赖
public class AlarmInitReceiver extends BroadcastReceiver {//这是一个主类
2 years ago
private static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.ALERTED_DATE
};
2 years ago
//调用标签ID和闹钟时间
2 years 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 years ago
long currentDate = System.currentTimeMillis(); //产生一个当前的毫秒自1970年1月1日0时起的毫秒数
2 years ago
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION,
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
2 years ago
new String[] { String.valueOf(currentDate) },//将currentDate从long转化为字符串
2 years ago
null);
2 years ago
//从数据库中找到和当前系统时间相等的标签
2 years ago
if (c != null) {
if (c.moveToFirst()) {
do {
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
Intent sender = new Intent(context, AlarmReceiver.class);
2 years ago
//新建intend
2 years ago
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
}
c.close();
}
2 years ago
//根据数据库里的闹钟时间创建一个闹钟机制
2 years ago
}
}