在安卓中如何设置日历提醒

设定权限

<!-- 闹钟设置权限 -->
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<!-- 日历权限 -->
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

显示日期选择和时间选择

创建DatePickerDialog、TimePickerDialog实例用于选择时间和日期
首先

private Calendar calendar;

实现函数

private void showDatePickerDialog() {
        // 创建DatePickerDialog实例
        calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(
                this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        // 更新全局的Calendar对象
                        calendar.set(year,month,dayOfMonth);
                        // 用户设置了日期后,可以在这里调用时间选择器
                        showTimePickerDialog(calendar);
                    }
                },
                year, month, day
        );
        datePickerDialog.show();
    }

    private void showTimePickerDialog(Calendar c) {
        // 创建TimePickerDialog实例
        TimePickerDialog timePickerDialog = new TimePickerDialog(
                this,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        calendar.set(Calendar.MINUTE, minute);
                        // 用户设置了时间后,可以在这里设置闹钟
                        setAlarm(calendar);
                    }
                },
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),
                true // 设置是否使用24小时制
        );
        timePickerDialog.show();
    }

用于发送提醒的类AlarmReceiver

需要继承BroadcastReceiver并重写 onReceive 方法。

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 从Intent中获取消息
        String message = intent.getStringExtra("message");

        // 创建一个通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "alarm_channel_id")
                .setSmallIcon(R.drawable.logo) // 通知小图标
                .setContentTitle("闹钟提醒") // 通知标题
                .setContentText(message) // 通知内容
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);


        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

        // 或者使用通知渠道(对于Android 8.0及以上)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("alarm_channel_id", "Alarm", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        // 发送通知
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification notification = builder.build();
            notificationManager.notify(3,notification);
        }
    }
}

设定闹钟setAlarm

private void setAlarm(Calendar calendar) {
        // 创建一个Intent,当闹钟时间到达时触发
        Intent intent = new Intent(this, AlarmReceiver.class);
        intent.putExtra("message", "闹钟时间到了!");

        // 创建一个PendingIntent,用于AlarmManager
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        // 获取AlarmManager服务
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        // 计算触发时间(毫秒)
        long triggerTime = calendar.getTimeInMillis();

        // 设置闹钟
        // AlarmManager.RTC_WAKEUP 表示使用实际时间,并且会唤醒设备
        alarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
    }

PendingIntent(待定意图)是一个用于在未来某个时间执行某项操作的基本组件。它封装了Intent对象及目标操作的上下文信息,可在稍后的时间点由系统触发。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值