Android 关于警报和通知的多个问题?
因此,我的目标是让我的应用程序在一天中的特定时间为用户创建通知。为此,我使用AlarmManager设置警报,然后当警报“熄灭”时,它将创建一个通知。下面是它的设置方式- 类别1-创建报警 类别2-创建通知 第3类-通知响应 我之所以创建这3个类,是因为在创建报警和通知时使用了“intent” 这是我的密码-Android 关于警报和通知的多个问题?,android,notifications,alarmmanager,Android,Notifications,Alarmmanager,因此,我的目标是让我的应用程序在一天中的特定时间为用户创建通知。为此,我使用AlarmManager设置警报,然后当警报“熄灭”时,它将创建一个通知。下面是它的设置方式- 类别1-创建报警 类别2-创建通知 第3类-通知响应 我之所以创建这3个类,是因为在创建报警和通知时使用了“intent” 这是我的密码- cal = Calendar.getInstance(); cal.add(Calendar.SECOND, 30); calintent = new Intent(thi
cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
calintent = new Intent(this, NotificationMaker.class);
calpendingintent = PendingIntent.getActivity(this, 12345, calintent, PendingIntent.FLAG_CANCEL_CURRENT);
am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), calpendingintent);
它呼吁NotificationMaker类,其中-
notifintent= new Intent(this, NotificationReceiver.class);
notifpendingintent = PendingIntent.getActivity(this, 0, notifintent, 0);
notif = new Notification.Builder(this)
.setContentTitle("Time To Smoke!")
.setContentIntent(notifpendingintent)
.addAction(R.drawable.background_teal, "Open App", notifpendingintent)
.addAction(R.drawable.background_red, "I smoked", notifpendingintent).build();
notifm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notif.flags |= Notification.PRIORITY_MAX;
notif.flags |= Notification.FLAG_NO_CLEAR;
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notifm.notify(0, notif);
正在发出通知。关于这一点,我有几个问题:
1) 我可以在不使用多个类的情况下执行我想要执行的两个任务吗?如果是,怎么做?(在同一类中发出警报和通知)。
2) 每当警报响起时,将打开一个空白布局,然后发出通知。我如何没有空白页面打开?
3) 是否有更有效的方法来执行这两项任务(发出警报以创建定时通知)
提前谢谢 据我所知,您正在使用一个活动来创建通知-最好使用BroadcastReceiver并使用AlarmManager启动一个具有待定意图的广播
我没有使用广播接收器。我在网上学习教程,不需要使用广播接收器。我正在alarm intent调用的类中创建通知。但是使用BroadcastReceiver不会使它变得更复杂吗?不-你正在重新启动一个活动,并且广播接收器更有效-代码也没有真正的不同。有关更多信息,请参阅本教程:好的,我们将研究它。谢谢