java - 在 for 循环中创建新 Intent 是好是坏?

标签 java android android-intent

我目前遇到一个问题,在 for 循环中创建新 Intent 是好是坏。我有以下情况:

1.

public static void reactivateReminders(Schedule schedule) {
    ArrayList<Lecture> allLectures = schedule.getAllLectures();

    for(Lecture lecture : allLectures) {
        ...
        // Set up various things for the reminder
        ...
        Intent intent = new Intent(getApplicationContext(), ReminderReceiver.class);
        String at = getResources().getString(R.string.at);
        String with = getResources().getString(R.string.with);
        String beginH = ScheduleHelper.formatNumber(changedBeginH);
        String beginM = ScheduleHelper.formatNumber(changedBeginM);
        String room = lecture.getRoom();
        intent.putExtra("contentText", at + " " + beginH + ":" + beginM + " in " + room + " " + with + " " + lecture.getLecturer());

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), lecture.getAlarmId(), intent, 0);//PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);

        if(lecture.getBeginH() != beginH || lecture.getBeginM() != beginM)
            alarm.cancel(pendingIntent);

        alarm.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis() + offset, 1000 * 60 * 60 * 24 * 7, pendingIntent);
    }
}

2.

public static void reactivateReminders(Schedule schedule) {
    ArrayList<Lecture> allLectures = schedule.getAllLectures();
    Intent intent = new Intent(getApplicationContext(), ReminderReceiver.class);

    for(Lecture lecture : allLectures) {
        ...
        // Set up various things for the reminder
        ...
        String at = getResources().getString(R.string.at);
        String with = getResources().getString(R.string.with);
        String beginH = ScheduleHelper.formatNumber(changedBeginH);
        String beginM = ScheduleHelper.formatNumber(changedBeginM);
        String room = lecture.getRoom();
        intent.putExtra("contentText", at + " " + beginH + ":" + beginM + " in " + room + " " + with + " " + lecture.getLecturer());

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), lecture.getAlarmId(), intent, 0);//PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);

        if(lecture.getBeginH() != beginH || lecture.getBeginM() != beginM)
            alarm.cancel(pendingIntent);

        alarm.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis() + offset, 1000 * 60 * 60 * 24 * 7, pendingIntent);
    }
}

哪个选项更好?我对 Java 不太熟悉,所以我不知道 Java 如何处理其中任何一个。也许根本没有什么区别,但由于我通常使用 C++ 编程,因此在循环内创建新对象让我担心。

感谢您提前提供的任何帮助。

编辑:结论:
正如 Alex Shutov 所提到的,最好不要一次设置所有提醒。用户可能只需要即将出现的下一个。

要实现此目的,您应该在应用程序中的某个位置设置最早的提醒,并将其他提醒(或者更确切地说,您用于提醒的数据)存储在应用程序外部的某个位置(XML、SQL...),以便您的服务可以在最早的提醒启动后读取文件以加载下一个提醒。

通过这样做,您就不会因为用户还不需要的提醒而给系统带来负担。我有时会尝试实现这个想法,但现在我将使用我的方法。

关于我的代码:
对于我发布的代码来说,更好的方法是在循环之外创建新 Intent 。由于我放入其中的额外内容具有相同的 key ,因此每次都会覆盖,并且您不必创建新的 Intent 。其他变量,例如“at”和“with”,它们是常量,也可以放置在循环之外。变量“beginH、beginM、room”可以删除,您可以直接在 putExtra 参数中调用函数。您还可以将 PendingIntent 和 AlarmManager 行放置在循环之外。

我会发布代码,但我认为我的帖子会太大。 感谢您的快速帮助:)

最佳答案

这是一个坏主意,因为您会因不必要的任务而使系统过载,您应该安排最近的事件,在 IntentService 中安排下一个事件

关于java - 在 for 循环中创建新 Intent 是好是坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39271476/

相关文章:

java - 括号中的整数无法编译 - 为什么?

java - 当我们允许注入(inject)时如何管理 ExecutorService 的关闭?

Android 更改适配器上的复选框无法正常工作

android - Gradle添加依赖项抛出方法dependencies.add

java - 将按钮链接到下一个 Android Activity 会导致应用程序崩溃

android - 将第 3 方 Android JAR 集成到 Titanium Mobile 应用程序中?

android - 获取插入后的自增值

java - 订阅事件总线

java - 具有外部类和内部类的数组

android-fragments - 替换 fragment 不会完全替换前一个 fragment 。为什么这样?