有关警报和通知的多个问题?

时间:2013-07-11 07:53:36

标签: android notifications alarmmanager

所以我的目标是让我的应用程序在一整天的特定时间为用户创建通知。为此,我使用AlarmManager设置警报,然后当警报“熄灭”时,它将创建一个通知。以下是设置方法 -

第1类 - 创建警报 第2类 - 创建通知 第3类 - 通知的回应

由于在创建警报和通知时使用“意图”,我已经完成了这3个课程。

这是我的代码 -

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)是否有更有效的方法来执行这两项任务(发出警报以创建定时通知)。

提前致谢!

1 个答案:

答案 0 :(得分:0)

据我所知,您正在使用Activity创建通知 - 更好的方法是使用BroadcastReciever并使用AlarmManager启动具有待处理意图的广播。

See here for more details on BroadcastReceiver

相关问题