Java 在循环中创建新的意图是好是坏?

Java 在循环中创建新的意图是好是坏?,java,android,android-intent,Java,Android,Android Intent,我现在的问题是,在for循环中创建一个新的意图是好是坏。我有以下情况: 1. public static void reactivateReminders(Schedule schedule) { ArrayList<Lecture> allLectures = schedule.getAllLectures(); for(Lecture lecture : allLectures) { ... // Set up various t

我现在的问题是,在for循环中创建一个新的意图是好是坏。我有以下情况:

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);
    }
}
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);
    }
}
公共静态无效重新激活管理员(时间表){
ArrayList AllCourts=schedule.getAllCourts();
用于(讲座:所有讲座){
...
//为提醒设置各种内容
...
Intent Intent=新的Intent(getApplicationContext(),rementerreceiver.class);
String at=getResources().getString(R.String.at);
String with=getResources().getString(R.String.with);
字符串beginH=scheduleheloper.formatNumber(changedBeginH);
字符串beginM=scheduleheloper.formatNumber(changedBeginM);
String-room=touch.getRoom();
intent.putExtra(“contentText”,at+“”+beginH+”:“+beginM+”在“+room+”“+with+“”+touch.get讲师()”);
PendingEvent PendingEvent=PendingEvent.getBroadcast(getApplicationContext(),teaching.getAlarmId(),intent,0);//PendingEvent.FLAG_UPDATE_CURRENT);
AlarmManager报警=(AlarmManager)getSystemService(报警服务);
如果(讲座.getBeginH()!=beginH | |讲座.getBeginM()!=beginM)
报警。取消(挂起);
alarm.setInexactRepeating(AlarmManager.RTC,calendar.getTimeInMillis()+偏移量,1000*60*60*24*7,PendingContent);
}
}
2.

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);
    }
}
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);
    }
}
公共静态无效重新激活管理员(时间表){
ArrayList AllCourts=schedule.getAllCourts();
Intent Intent=新的Intent(getApplicationContext(),rementerreceiver.class);
用于(讲座:所有讲座){
...
//为提醒设置各种内容
...
String at=getResources().getString(R.String.at);
String with=getResources().getString(R.String.with);
字符串beginH=scheduleheloper.formatNumber(changedBeginH);
字符串beginM=scheduleheloper.formatNumber(changedBeginM);
String-room=touch.getRoom();
intent.putExtra(“contentText”,at+“”+beginH+”:“+beginM+”在“+room+”“+with+“”+touch.get讲师()”);
PendingEvent PendingEvent=PendingEvent.getBroadcast(getApplicationContext(),teaching.getAlarmId(),intent,0);//PendingEvent.FLAG_UPDATE_CURRENT);
AlarmManager报警=(AlarmManager)getSystemService(报警服务);
如果(讲座.getBeginH()!=beginH | |讲座.getBeginM()!=beginM)
报警。取消(挂起);
alarm.setInexactRepeating(AlarmManager.RTC,calendar.getTimeInMillis()+偏移量,1000*60*60*24*7,PendingContent);
}
}
哪个选项更好?我对Java不太熟悉,所以我不知道Java如何处理这两个问题。也许是没有什么区别,但由于我通常在C++编程,所以在循环中创建新对象会让我担心。 提前谢谢你的帮助

编辑:结论
正如Alex Shutov所提到的,最好不要同时设置所有提醒。用户可能只需要接下来的一个

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

这样做,您就不会给系统增加用户甚至还不需要的提醒。我会尝试在某个时候实现这个想法,但现在我将使用我的方法

关于我的代码:
对于我发布的代码,更好的方法是在循环之外创建新的意图。由于我输入的额外内容具有相同的密钥,因此每次都会覆盖,您不必创建新的意图。其他变量,如my“at”和“with”,都是常量,也可以放在循环之外。变量“beginH,beginM,room”可以删除,您可以直接在putExtra参数中调用函数。您还可以将PendingEvent和AlarmManager线放置在循环外部

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

这是个坏主意,因为您让系统超负荷执行不必要的任务,所以您应该安排最近的事件,在IntentService中安排下一个事件

是否需要循环?因为您只使用最后一个元素,为什么不直接使用它呢?以任何方式在for循环内部创建变量都不是一个好方法。只有在你需要的时候才去做这真是个坏主意@Prashant感谢您提供的变量信息。创建我在循环外部使用的所有变量,然后在循环内部设置它们是否更好?我也不明白你的第一句话。你能解释一下吗?我也这么想。。我刚刚检查了我的应用程序的内存使用情况,它使用了大约50MB的内存,尽管我只有一个时间表,上面有几个讲座和提醒。你的建议有没有一个小例子?我不明白你的意思。我的意思是,你启动IntentService,然后从数据库加载当前通知,处理它,然后它计算下一个通知(IntentService在后台线程上运行)并安排下一次更新。之后,它自动停止。请参阅本指南中的示例对不起,我仍然不明白你的意思。。您建议我将设置警报的代码放入服务中吗?不,您应该创建一些类,它可以保存和删除任务,并执行最近的任务并安排它。保存所有任务,然后将第一个任务安排在应用程序中的某个位置。该服务将在首次活动时启动。在它里面,做一些工作,安排下一个任务,我想我现在明白了。您建议在我的应用程序中的某个位置设置第一个提醒,并在收到提醒时设置