2024.1.18 将已经做好的内容补充一下

main
YPL233 11 months ago
parent 8fcc16311f
commit e4c1a5c113

@ -27,39 +27,56 @@ import android.database.Cursor;
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
/**
* 广
*/
public class AlarmInitReceiver extends BroadcastReceiver { public class AlarmInitReceiver extends BroadcastReceiver {
private static final String [] PROJECTION = new String [] { private static final String[] PROJECTION = new String[]{
NoteColumns.ID, NoteColumns.ID,
NoteColumns.ALERTED_DATE NoteColumns.ALERTED_DATE
}; };
private static final int COLUMN_ID = 0; private static final int COLUMN_ID = 0;
private static final int COLUMN_ALERTED_DATE = 1; private static final int COLUMN_ALERTED_DATE = 1;
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
// 获取当前时间
long currentDate = System.currentTimeMillis(); long currentDate = System.currentTimeMillis();
// 查询数据库,获取需要提醒的备忘录
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI, Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION, PROJECTION,
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE, NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
new String[] { String.valueOf(currentDate) }, new String[]{String.valueOf(currentDate)},
null); null);
if (c != null) { if (c != null) {
if (c.moveToFirst()) { if (c.moveToFirst()) {
do { do {
// 获取提醒时间和备忘录的ID
long alertDate = c.getLong(COLUMN_ALERTED_DATE); long alertDate = c.getLong(COLUMN_ALERTED_DATE);
long noteId = c.getLong(COLUMN_ID);
// 创建一个新的Intent对象并设置要启动的AlarmReceiver类
Intent sender = new Intent(context, AlarmReceiver.class); Intent sender = new Intent(context, AlarmReceiver.class);
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID))); // 设置Intent的数据为备忘录的URI
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId));
// 创建一个PendingIntent对象用于将Intent发送给AlarmReceiver类
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE); // 获取系统的AlarmManager服务
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// 设置闹钟当提醒时间到达时发送PendingIntent
alarmManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext()); } while (c.moveToNext());
} }
c.close(); c.close();
} }
} }
} }

@ -20,11 +20,17 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
/*
广
*/
public class AlarmReceiver extends BroadcastReceiver { public class AlarmReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
// 创建一个新的Intent对象并设置要启动的Activity
intent.setClass(context, AlarmAlertActivity.class); intent.setClass(context, AlarmAlertActivity.class);
// 为该Intent添加一个标志位表示要以新的Task启动Activity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 启动指定的Activity
context.startActivity(intent); context.startActivity(intent);
} }
} }

@ -29,22 +29,37 @@ import android.content.DialogInterface.OnClickListener;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.text.format.DateUtils; import android.text.format.DateUtils;
/**
*
* DateTimePickerDialog
* AlertDialog
* OnClickListener
*
*/
public class DateTimePickerDialog extends AlertDialog implements OnClickListener { public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
//声明了一个Calendar对象mDate用于存储选择的日期和时间。
private Calendar mDate = Calendar.getInstance(); private Calendar mDate = Calendar.getInstance();
//声明了一个boolean类型的变量mIs24HourView用于表示是否使用24小时制。
private boolean mIs24HourView; private boolean mIs24HourView;
//声明了一个OnDateTimeSetListener接口用于监听日期和时间的设置。
private OnDateTimeSetListener mOnDateTimeSetListener; private OnDateTimeSetListener mOnDateTimeSetListener;
//声明了一个DateTimePicker对象mDateTimePicker用于显示日期和时间选择器。
private DateTimePicker mDateTimePicker; private DateTimePicker mDateTimePicker;
//功能定义了一个OnDateTimeSetListener接口其中包含一个OnDateTimeSet()方法,用于在日期和时间被设置时回调。
public interface OnDateTimeSetListener { public interface OnDateTimeSetListener {
void OnDateTimeSet(AlertDialog dialog, long date); void OnDateTimeSet(AlertDialog dialog, long date);
} }
//功能DateTimePickerDialog的构造函数接收一个Context对象和一个long类型的date参数。
public DateTimePickerDialog(Context context, long date) { public DateTimePickerDialog(Context context, long date) {
//调用父类AlertDialog的构造函数。
super(context); super(context);
//创建一个DateTimePicker对象并设置为对话框的视图。
mDateTimePicker = new DateTimePicker(context); mDateTimePicker = new DateTimePicker(context);
setView(mDateTimePicker); setView(mDateTimePicker);
//设置DateTimePicker的OnDateTimeChangedListener当日期和时间发生改变时会回调onDateTimeChanged()方法。
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() { mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
//将给定的date参数设置给mDate同时将秒数设置为0并将当前的日期和时间设为DateTimePicker的当前选中值。
public void onDateTimeChanged(DateTimePicker view, int year, int month, public void onDateTimeChanged(DateTimePicker view, int year, int month,
int dayOfMonth, int hourOfDay, int minute) { int dayOfMonth, int hourOfDay, int minute) {
mDate.set(Calendar.YEAR, year); mDate.set(Calendar.YEAR, year);
@ -55,23 +70,29 @@ public class DateTimePickerDialog extends AlertDialog implements OnClickListener
updateTitle(mDate.getTimeInMillis()); updateTitle(mDate.getTimeInMillis());
} }
}); });
//设置对话框的第一个按钮的文本为确定,并设置点击监听为当前类。
mDate.setTimeInMillis(date); mDate.setTimeInMillis(date);
//设置对话框的第二个按钮的文本为取消,点击监听为空。
mDate.set(Calendar.SECOND, 0); mDate.set(Calendar.SECOND, 0);
//根据系统设置的时间制式设置是否使用24小时制。
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
//更新对话框的标题,显示当前选中的日期和时间。
setButton(context.getString(R.string.datetime_dialog_ok), this); setButton(context.getString(R.string.datetime_dialog_ok), this);
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null); setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
set24HourView(DateFormat.is24HourFormat(this.getContext())); set24HourView(DateFormat.is24HourFormat(this.getContext()));
updateTitle(mDate.getTimeInMillis()); updateTitle(mDate.getTimeInMillis());
} }
//设置是否使用24小时制。
public void set24HourView(boolean is24HourView) { public void set24HourView(boolean is24HourView) {
mIs24HourView = is24HourView; mIs24HourView = is24HourView;
} }
//设置一个OnDateTimeSetListener对象用于监听日期和时间的设置。
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) { public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
mOnDateTimeSetListener = callBack; mOnDateTimeSetListener = callBack;
} }
//定义了一个私有方法updateTitle(),用于更新对话框的标题,根据给定的日期和时间。
private void updateTitle(long date) { private void updateTitle(long date) {
int flag = int flag =
DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_YEAR |

@ -27,14 +27,18 @@ import android.widget.PopupMenu.OnMenuItemClickListener;
import net.micode.notes.R; import net.micode.notes.R;
/*
*/
public class DropdownMenu { public class DropdownMenu {
private Button mButton; private Button mButton;//定义了一个按钮对象
private PopupMenu mPopupMenu; private PopupMenu mPopupMenu;//定义弹出菜单
private Menu mMenu; private Menu mMenu;//菜单对象
public DropdownMenu(Context context, Button button, int menuId) { public DropdownMenu(Context context, Button button, int menuId) {
mButton = button; mButton = button;
mButton.setBackgroundResource(R.drawable.dropdown_icon); mButton.setBackgroundResource(R.drawable.dropdown_icon);//设置按钮的样式
mPopupMenu = new PopupMenu(context, mButton); mPopupMenu = new PopupMenu(context, mButton);
mMenu = mPopupMenu.getMenu(); mMenu = mPopupMenu.getMenu();
mPopupMenu.getMenuInflater().inflate(menuId, mMenu); mPopupMenu.getMenuInflater().inflate(menuId, mMenu);
@ -51,11 +55,11 @@ public class DropdownMenu {
} }
} }
public MenuItem findItem(int id) { public MenuItem findItem(int id) {//方法接受一个id参数表示要查找的菜单项的ID。
return mMenu.findItem(id); return mMenu.findItem(id);
} }
public void setTitle(CharSequence title) { public void setTitle(CharSequence title) {//设置按钮的标题
mButton.setText(title); mButton.setText(title);
} }
} }

Loading…
Cancel
Save