forループの中に新しいインテントを作成するのが良いか悪いのか、私は現在この質問に悩まされています。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);
}
}
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++で普通にプログラミングしているので、ループ内に新しいオブジェクトを作成すると心配してしまうかもしれません。
ご協力いただきありがとうございます。
編集:結論:
アレックスShutovで述べたように、それはより良い一度にすべてのアラームを設定しないようにします。ユーザーはおそらく次のものが来るだけです。
これを達成するには、アプリのどこかで最も早いリマインダーを設定し、アプリ外の場所(XML、SQLなど)でソートされた他のリマインダー(またはそれに使用するデータ)を保存する必要がありますあなたのサービスは、最も早いリマインダーが始まった後、次のファイルを読み込むためにファイルを読むことができます。
このようにすると、ユーザーがまだ必要ではないことを知らせるシステムに負担をかけることはありません。私はいつかこのアイデアを実装しようとしますが、私は私のアプローチを使用します。私のコードについて
:
私の投稿のコードのためのより良いアプローチは、ループの外に一度新しい意図を作成することです。余分なものは同じキーを持っているので、毎回上書きされ、新しいインテントを作成する必要はありません。私の "at"や "with"のような他の変数は定数であり、ループの外側に配置することもできます。変数 "beginH、beginM、room"は削除することができ、putExtraパラメータで直接関数を呼び出すことができます。ループの外側にPendingIntentとAlarmManager行を配置することもできます。
私はコードを投稿しますが、私の投稿は大きすぎると思います。ファストヘルプ:)
に、最寄りのイベントのスケジュールを設定する必要があり、悪い考えであるため おかげで、それはをループする必要がありますか?最後の要素だけを使用しているので、なぜそれを直接使用しないのですか? – Prashant
forループ内の変数を作成するのは良い方法ではありません。あなたが実際にそれを必要とする場合にのみそれをしなさい – Prashant
それは本当に悪い考えです! – Piyush