2016-09-01 4 views
2

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行を配置することもできます。

私はコードを投稿しますが、私の投稿は大きすぎると思います。ファストヘルプ:)

+0

に、最寄りのイベントのスケジュールを設定する必要があり、悪い考えであるため おかげで、それはをループする必要がありますか?最後の要素だけを使用しているので、なぜそれを直接使用しないのですか? – Prashant

+0

forループ内の変数を作成するのは良い方法ではありません。あなたが実際にそれを必要とする場合にのみそれをしなさい – Prashant

+0

それは本当に悪い考えです! – Piyush

答えて

3

それはあなたが不必要なタスクとシステムをオーバーロードするので、あなたが代わりにIntentServiceスケジュール次のイベント

+0

私はちょうど2つの講義とリマインダーで1つのスケジュールを持っていたが、私はちょうど私のアプリのRAM使用量をチェックし、約50 MBを使用します。あなたの提案のための小さな例がありますか?私はあなたが意味することを理解していません。 – Urasam

+0

つまり、BroadcastReceiverはIntentServiceを起動し、現在の通知をデータベースからロードして処理し、次の通知(バックグラウンドスレッドで実行されるIntentService)を計算し、次回の更新をスケジュールします。その後、自律的に停止します。このガイドの例を参照してくださいhttp://www.vogella。com/tutorials/AndroidServices/article.html –

+0

申し訳ありませんが、私はまだあなたが意味するものは得られません。サービスにアラームを設定した場所にコードを置くことをお勧めしますか? – Urasam

関連する問題