-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
48 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/src/main/java/com/mohammedsazid/android/aiub/NoticeAlarmReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.mohammedsazid.android.aiub; | ||
|
||
import android.app.AlarmManager; | ||
import android.app.PendingIntent; | ||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Build; | ||
import android.os.SystemClock; | ||
import android.util.Log; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static android.content.Context.ALARM_SERVICE; | ||
|
||
public class NoticeAlarmReceiver extends BroadcastReceiver { | ||
|
||
// reschedule every one hour | ||
private static final long REPEAT_INTERVAL = 1; | ||
public static final String CHECK_NOTICE_ACTION = "com.mohammedsazid.intent.action.CHECK_NOTICE"; | ||
|
||
private static PendingIntent getPendingIntent(Context context) { | ||
Intent intent = new Intent(); | ||
intent.setClass(context, NoticeAlarmReceiver.class); | ||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); | ||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
intent.setAction(CHECK_NOTICE_ACTION); | ||
|
||
return PendingIntent.getBroadcast(context, 0, intent, 0); | ||
} | ||
|
||
public static void scheduleNewCheck(Context context) { | ||
long deferTime = TimeUnit.HOURS.toMillis(REPEAT_INTERVAL); | ||
long when = SystemClock.elapsedRealtime() + deferTime; | ||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE); | ||
|
||
PendingIntent pi = getPendingIntent(context); | ||
|
||
if (alarmManager != null) { | ||
// cancel any previous alarm | ||
alarmManager.cancel(pi); | ||
|
||
int SDK_INT = Build.VERSION.SDK_INT; | ||
if (SDK_INT < Build.VERSION_CODES.KITKAT) | ||
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, pi); | ||
else if (SDK_INT < Build.VERSION_CODES.M) | ||
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, pi); | ||
else | ||
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, pi); | ||
} | ||
} | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
NoticeCheckJobIntentService.startActionCheckNotice(context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters