多个本地通知android

 渊博的效力 发布于 2023-02-11 11:52

下午好.

我希望在我的应用中获得多个本地通知,所以我有

在我的Activity中,此方法应将警报设置为17:20,17:21,17:22,但我在17:22只有一个通知.

public void multiplyAlerts(){

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(this, LocalNotification.class);

    for(int i = 0; i < 3; i++){

        Calendar t_calendar = Calendar.getInstance();

        t_calendar.set(Calendar.MONTH, Calendar.DECEMBER);
        t_calendar.set(Calendar.YEAR, 2013);
        t_calendar.set(Calendar.DAY_OF_MONTH, 12);

        t_calendar.set(Calendar.HOUR_OF_DAY, 17);
        t_calendar.set(Calendar.MINUTE, 20 + i);
        t_calendar.set(Calendar.SECOND, 23);
        t_calendar.set(Calendar.AM_PM, Calendar.PM);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        alarmManager.set(AlarmManager.RTC_WAKEUP, t_calendar.getTimeInMillis(),pendingIntent);

        System.out.println("Calling Alarm  " + i);

    }}

本地通知类

public class LocalNotification extends BroadcastReceiver {

NotificationManager nm;

@Override
public void onReceive(Context context, Intent intent) {

    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence from = "Remsmed";
    CharSequence message = "????????? ???-??";
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    Notification notif = new Notification(R.drawable.launcher,"Notification text", System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, pendingIntent);
    notif.flags = Notification.FLAG_AUTO_CANCEL;
    nm.notify((int)System.currentTimeMillis(),notif);

    System.out.println("id = " + System.currentTimeMillis());
}}

我将此字符串添加到清单中


谢谢你的答案!

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有