如何在指定时间段内显示通知中的字符串列表?

问题描述 投票:0回答:1

我想在指定的时间内显示通知。就像我有一个开始时间,当我想看到的通知和结束时间,直到当我想看到的通知,即字符串的列表,应该显示在一个给定的时间段。

此外,该列表可以是用户指定的任何数量。

我怎样才能动态地决定显示通知的时间?或者说如何统一划分时间段和字符串?

为了更清楚地说明,这里是显示通知的开始时间、结束时间和显示字符串数量的屏幕。

enter image description here

请帮助我 谢谢你...

EDIT :

我正在尝试给出的解决方案。

 List<String> times = new ArrayList<>();
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);
            long minutes = ((end.getTime() - start.getTime()) / 1000 / 60) /
                    howMany;
            for (int i = 0; i < howMany; i++) {

                Calendar calobj = Calendar.getInstance();
                calobj.setTime(start);
                calobj.add(Calendar.MINUTE, (int) (i * minutes));
                String time = dateFormat.format(calobj.getTime());
                times.add(time);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d("timesList", times.toString());
        return times;
    }

    public static void showNotification(
            List<String> timeList, Context context,
            String quote
    ) {

        Intent notifyIntent = new Intent(context, MyNewIntentReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                notifyIntent, PendingIntent.FLAG_ONE_SHOT
        );

        notifyIntent.putExtra("title", context.getString(R.string.app_name));

        AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);

        for (String time : timeList) {
            final int random = new Random().nextInt();
            notifyIntent.putExtra("notify_id", random);

            notifyIntent.putExtra(
                    "quote",
                    quote
            );
            Date date;
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
            try {
              date = dateFormat.parse(time);
                System.out.println(date);

            alarmManager
                    .setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                            date.getTime(),
                            date.getTime(),
                            pendingIntent
                    );

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        Log.d("notificationIntentSet", "Utils, pending intent set");
    }

在我的接收器中建立通知。

  public class MyNewIntentReceiver extends BroadcastReceiver {

    public MyNewIntentReceiver() {
    }


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

        PowerManager powerManager = (PowerManager) context.getSystemService(
                Context.POWER_SERVICE);
        PowerManager.WakeLock wakeLock =
                powerManager.newWakeLock(
                        PowerManager.PARTIAL_WAKE_LOCK,
                        "dailyfaith:wakelog"
                );
        wakeLock.acquire();

        // get id, titleText and bigText from intent
        int NOTIFY_ID = intent.getIntExtra("notify_id", 0);
        String titleText = intent.getStringExtra("title");
        String bigText = intent.getStringExtra("quote");

        // Create intent.
        Intent notificationIntent = new Intent(context, MainActivity.class);

        // use NOTIFY_ID as requestCode
        PendingIntent contentIntent = PendingIntent.getActivity(context,
                NOTIFY_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT
        );

        // get res.
        Resources res = context.getResources();

        // build notification.
        Notification.Builder builder = new Notification.Builder(context)
                .setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.ic_daily_faith_icon)
                .setAutoCancel(true)
                .setContentTitle(titleText)
                .setSound(RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentText(bigText);

        Log.d("notificationBuild", "Notification Builder set");

    /*    // check vibration.
        if (mPrefs.getBoolean("vibration", true)) {
            builder.setVibrate(new long[]{0, 50});
        }*/

   /*     // create default title if empty.
        if (titleText.equals("")) {
            builder.setContentTitle(
                    context.getString(R.string.app_name));
        }*/

        // show notification. check for delay.
        builder.setWhen(System.currentTimeMillis());
        Log.d("notificationSetWhen", "Notification set when triggered");

        Notification notification = new Notification.BigTextStyle(builder)
                .bigText(bigText).build();

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFY_ID, notification);

        wakeLock.release();
    }
}

从Activity :

  @Override
    public void onTimeSet(
            TimePickerDialog view, int hourOfDay, int minute, int second
    ) {
        String hourString = hourOfDay < 10 ? "0" + hourOfDay : "" + hourOfDay;
        String minuteString = minute < 10 ? "0" + minute : ":" + minute;
        String time = hourString + minuteString;

        if (startTimeSelected) {
            startTime = time;
            textViewStartTime.setText(time);
        }
        else if (endTimeSelected) {
            endTime = time;
            textViewEndTime.setText(time);
        }

        String count = (String) textViewQuoteCount.getText();
        count.replace("X","");

        if(startTimeSelected && endTimeSelected)
        {
            Utils.setAlarmTimeList(startTime, endTime, Integer.parseInt(count));
            Utils.showNotification(timeList); // not sure how to send the list of strings - quotes
        }

        tpd = null;
    }

我将时间数组传递给待定意图,但通知没有被触发。我想,对于报警,我也需要给出当前日期,所以我再次为每个通知格式化了时间。

但这也没有用。有什么建议吗?

EDIT :

我已经更新了Erwin的答案。我现在也得到了日期和时间,但是我调试时,接收器也没有被调用。

我已经在清单文件中设置了接收器。

<receiver
    android:name = ".MyNewIntentReceiver"
    android:enabled = "true"
    android:exported = "false" />

Log of timesList

  D/timesList: [Tue May 19 16:21:00 GMT+05:30 2020, Tue May 19 16:24:00 GMT+05:30 2020, Tue May 19 16:27:00 GMT+05:30 2020, Tue May 19 16:30:00 GMT+05:30 2020, Tue May 19 16:33:00 GMT+05:30 2020, Tue May 19 16:36:00 GMT+05:30 2020, Tue May 19 16:39:00 GMT+05:30 2020, Tue May 19 16:42:00 GMT+05:30 2020, Tue May 19 16:45:00 GMT+05:30 2020, Tue May 19 16:48:00 GMT+05:30 2020]

会是什么问题呢。

我试着把当前的时间给待定的意图作为..:

 alarmManager
                .setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        System.currentTimeMillis(),
                        System.currentTimeMillis(),
                        pendingIntent
                );

然后我得到了通知。但没有得到当我设置一个日期到。

EDIT 2

   public static List<Date> setAlarmTimeList(String startTime, String endTime, int howMany) {
        List<Date> times = new ArrayList<>();
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);
            long minutes = ((end.getTime() - start.getTime()) / 1000 / 60) /
                    (howMany - 1);
            Calendar calobj;
            for (int i = 0; i < howMany; i++) {

                calobj = Calendar.getInstance();
                calobj.set(Calendar.HOUR_OF_DAY, Integer.valueOf(dateFormat.format(start).split(":")[0]));
                calobj.set(Calendar.MINUTE, Integer.valueOf(dateFormat.format(start).split(":")[1]));
                calobj.add(Calendar.MINUTE, (int) (i * minutes));
                calobj.set(Calendar.SECOND, 0);
                times.add(calobj.getTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d("timesList", times.toString());
        return times;
    }


    public static void showNotification(
            List<Date> timeList, Context context,
            String quote
    ) {

        for (Date date : timeList) {
            Intent notifyIntent = new Intent(context, MyNewIntentReceiver.class);

            notifyIntent.putExtra("title", context.getString(R.string.app_name));

            final int random = new Random().nextInt();
            notifyIntent.putExtra("notify_id", random);

            notifyIntent.putExtra(
                    "quote",
                    quote
            );
            int randomInt = new Random().nextInt(1000);

            notifyIntent.putExtra("requestCode",randomInt);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                    randomInt,
                    notifyIntent, PendingIntent.FLAG_ONE_SHOT

            );

            AlarmManager alarmManager = (AlarmManager) context
                    .getSystemService(Context.ALARM_SERVICE);


            Log.d("date",String.valueOf(date.getTime()));

         /*   long afterTwoMinutes = SystemClock.elapsedRealtime() + 60 * 1000;*/
            long afterTwoMinutes = System.currentTimeMillis();

            Log.d("aftertwoMinutes",String.valueOf(afterTwoMinutes));

            long datetimer = date.getTime();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
                alarmManager.setExactAndAllowWhileIdle
                        (AlarmManager.ELAPSED_REALTIME_WAKEUP,
                               date.getTime(), pendingIntent);
            else
                alarmManager.setExact
                        (AlarmManager.ELAPSED_REALTIME_WAKEUP,
                                date.getTime(), pendingIntent);
        }

        Log.d("notificationIntentSet", "Utils, pending intent set");
    }


public class MyNewIntentReceiver extends BroadcastReceiver {

    public MyNewIntentReceiver() {
    }


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

        int NOTIFY_ID = intent.getIntExtra("notify_id", 0);
        String titleText = intent.getStringExtra("title");
        String bigText = intent.getStringExtra("quote");
        int requestCode = intent.getIntExtra("requestCode",0);
        sendNotification(context,bigText,NOTIFY_ID,requestCode);
    }

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library

    }

    public static void sendNotification(Context mcontext, String messageBody,
            int notify_id,int requestCode) {
        Intent intent = new Intent(mcontext, HomeScreenActivity.class);
        PendingIntent pendingIntent = PendingIntent
                .getActivity(mcontext, requestCode /* Request code */, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        NotificationManager notificationManager = (NotificationManager) mcontext
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Uri defaultSoundUri = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel
                    (
                            mcontext.getString(R.string.default_notification_channel_id),
                            "Rewards Notifications",
                            NotificationManager.IMPORTANCE_HIGH
                    );

            // Configure the notification channel.
            notificationChannel.setDescription("Channel description");
            notificationChannel.enableLights(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat
                .Builder(mcontext, mcontext.getString(R.string.default_notification_channel_id))
                .setContentTitle(mcontext.getString(R.string.app_name))
                .setSmallIcon(R.drawable.ic_daily_faith_icon)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);


        notificationManager.notify(notify_id /* ID of notification */,
                notificationBuilder.build());
    }
}

对date.getTime()不起作用,对SystemClock起作用的是System.currentTimeInMilliseconds()。

java android android-notifications alarmmanager
1个回答
4
投票

试试这个。

    public static List<Date> setAlarmTimeList(String startTime, String endTime, int howMany) {
    List<Date> times = new ArrayList<>();
    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
        Date start = dateFormat.parse(startTime);
        Date end = dateFormat.parse(endTime);
        long minutes = ((end.getTime() - start.getTime()) / 1000 / 60) /
                (howMany - 1);
        Calendar calobj;
        for (int i = 0; i < howMany; i++) {

            calobj = Calendar.getInstance();
            calobj.set(Calendar.HOUR_OF_DAY, Integer.valueOf(dateFormat.format(start).split(":")[0]));
            calobj.set(Calendar.MINUTE, Integer.valueOf(dateFormat.format(start).split(":")[1]));
            calobj.add(Calendar.MINUTE, (int) (i * minutes));
            calobj.set(Calendar.SECOND, 0);
            times.add(calobj.getTime());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    Log.d("timesList", times.toString());
    return times;
}

以毫秒为单位获取开始时间和结束时间,然后除以多少次。

为报警管理器。

public static void showNotification(List<Date> timeList, Context context, String quote) {

        Intent notifyIntent = new Intent(context, MyNewIntentReceiver.class);

        notifyIntent.putExtra("title", context.getString(R.string.app_name));

        AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);

        for (Date time : timeList) {
            final int random = new Random().nextInt();
            notifyIntent.putExtra("notify_id", random);

            notifyIntent.putExtra(
                    "quote",
                    quote
            );

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, random,
                    notifyIntent, PendingIntent.FLAG_ONE_SHOT
            );

            alarmManager
                    .setInexactRepeating(AlarmManager.RTC_WAKEUP,
                            time.getTime(),
                            AlarmManager.INTERVAL_DAY,
                            pendingIntent
                    );
        }

        Log.d("notificationIntentSet", "Utils, pending intent set");
    }
© www.soinside.com 2019 - 2024. All rights reserved.