Android 从通知设置警报
我需要在手机的时钟应用程序中添加一个闹钟,并向用户发送通知。当用户单击通知时,应在给定时间内添加新警报。 代码如下:Android 从通知设置警报,android,notifications,android-pendingintent,alarm,Android,Notifications,Android Pendingintent,Alarm,我需要在手机的时钟应用程序中添加一个闹钟,并向用户发送通知。当用户单击通知时,应在给定时间内添加新警报。 代码如下: //Create intent Intent alarmIntent = new Intent(AlarmClock.ACTION_SET_ALARM); alarmIntent.putExtra(AlarmClock.EXTRA_MESSAGE, event.getEventName()); Calendar alarmTime = new GregorianCalendar(
//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)代码>似乎什么也没做。当用户单击通知时,您必须在应用程序中使用广播接收器来接收广播
让您的广播接收器为NotifBroadCastReceiver
public class NotifBroadCastReceiver extends BroadcastReceiver{
@override
void onReceive(Context context, Intent intent){
//you can extract info using intent.getStringExtra or any other method depending on your send data type. After that set alarm here.
}
}
因此,当创建挂起的意图时,您可以这样做
Intent intent = new Intent(context, BroadcastReceiver.class);
//set all the info you needed to set alarm like time and other using putExtra.
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
现在,当用户单击通知时,您将在NotifBroadCastReceiver的onReceive中接收广播
注意您必须在清单中注册广播接收器
<receiver
android:name="your broadcast receiver"
android:enabled="true"
android:exported="false" />
当用户单击通知时,您必须在应用程序中使用广播接收器来接收广播
让您的广播接收器为NotifBroadCastReceiver
public class NotifBroadCastReceiver extends BroadcastReceiver{
@override
void onReceive(Context context, Intent intent){
//you can extract info using intent.getStringExtra or any other method depending on your send data type. After that set alarm here.
}
}
因此,当创建挂起的意图时,您可以这样做
Intent intent = new Intent(context, BroadcastReceiver.class);
//set all the info you needed to set alarm like time and other using putExtra.
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
现在,当用户单击通知时,您将在NotifBroadCastReceiver的onReceive中接收广播
注意您必须在清单中注册广播接收器
<receiver
android:name="your broadcast receiver"
android:enabled="true"
android:exported="false" />
如果要使用AlarmClock.ACTION\u set\u alarm
设置闹钟,则必须使用PendingEvent.GetActivity()
而不是PendingEvent.getBroadcast()
<代码>报警时钟。操作设置报警
是一项活动
操作
如果不想显示闹钟的UI,可以将其添加到意图中:
alarmIntent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
如果要使用AlarmClock.ACTION\u set\u alarm
设置闹钟,则必须使用PendingEvent.GetActivity()
而不是PendingEvent.getBroadcast()
<代码>报警时钟。操作设置报警
是一项活动
操作
如果不想显示闹钟的UI,可以将其添加到意图中:
alarmIntent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
您必须在pendingent.getBroadcast()中使用显式意图。您能解释一下吗?您必须在pendingent.getBroadcast()中使用显式意图。您能解释一下吗?