如何為for循環中生成的所有日期設置通知

[英]how do i set notifications for all dates generated in a for loop


    String date = "28/04/2018";
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Calendar c = Calendar.getInstance();
    try {
        c.setTime(sdf.parse(date));
    } catch (ParseException ex) {
        Logger.getLogger(PondDaily.class.getName()).log(Level.SEVERE, null, ex);
    }
    int maxDay = c.getActualMaximum(Calendar.DAY_OF_YEAR);
    for (int co = 0; co < maxDay; co++) {
        System.out.println(sdf.format(c.getTime()));
        c.add(Calendar.DATE, 21);

    }

this is my code am generating all dates 21 days after the start date thanks

這是我的代碼我在開始日期后21天生成所有日期謝謝

1 个解决方案

#1


1  

MainActivity.class

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
          scheduleNotification(getNotification("text to be displayed"), calendar);


        }


    private void scheduleNotification(Notification notification, Calendar calendar) {

                Intent notificationIntent = new Intent(this, NotificationNotify.class);
                notificationIntent.putExtra(NotificationNotify .NOTIFICATION_ID, 1);
                notificationIntent.putExtra(NotificationNotify .NOTIFICATION, notification);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

         AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            }

            private Notification getNotification(String content) {
                Notification.Builder builder = new Notification.Builder(this);
                builder.setContentTitle("Scheduled Notification");
                builder.setContentText(content);
                builder.setSmallIcon(R.drawable.ic_launcher);
                return builder.build();
            }

Broadcast Receiver

public class NotificationNotify extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification-id";
    public static String NOTIFICATION = "notification";

    public void onReceive(Context context, Intent intent) {

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        int id = intent.getIntExtra(NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification);

    }
}

Android Mainfest

        <receiver android:name=".NotificationNotify " />

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/04/28/251fa35af8c2c8f70d9841b452eebbe4.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com