gpt4 book ai didi

android - 从通知设置闹钟

转载 作者:行者123 更新时间:2023-11-30 04:59:30 26 4
gpt4 key购买 nike

我需要向移动设备的时钟应用程序添加闹钟,并为此向用户发送通知。当用户点击通知时,应在给定时间添加新警报。下面是代码:

//Create intent
Intent alarmIntent = new Intent(AlarmClock.ACTION_SET_ALARM);
alarmIntent.putExtra(AlarmClock.EXTRA_MESSAGE, event.getEventName());
Calendar alarmTime = new GregorianCalendar();
alarmTime.setTime(new Date(event.getAlarmTime()));
alarmIntent.putExtra(AlarmClock.EXTRA_HOUR, alarmTime.get(Calendar.HOUR_OF_DAY));
alarmIntent.putExtra(AlarmClock.EXTRA_MINUTES, alarmTime.get(Calendar.MINUTE));
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);

//Create and show notification
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("MyAppsAlarm",
"MyAppsAlarmNotifications",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Channel to show notifs");
mNotificationManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(main.getApplicationContext(), "Zzzzz")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Alarm Helper")
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(alarmPendingIntent);
mNotificationManager.notify(0, builder.build());

当我点击通知时没有任何反应。当通知抽屉自动关闭时,通知保持原样。

我尝试使用 startActivity(alarmIntent); 触发 intent,它按预期工作,但来自通知 .setContentIntent(alarmPendingIntent);似乎什么都不做。

最佳答案

如果您想使用 AlarmClock.ACTION_SET_ALARM 设置闹钟,则必须使用 PendingIntent.getActvity() 而不是 PendingIntent.getBroadcast()AlarmClock.ACTION_SET_ALARM 是一个 Activity ACTION。

如果不想显示闹钟的UI,可以在Intent中添加:

alarmIntent.putExtra(AlarmClock.EXTRA_SKIP_UI, true); 

关于android - 从通知设置闹钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58483972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com