I have an application that needs to update / receive data from the server every six hours. To do this, I created AlarmManager as follows:
public class Repository {
public static AlarmManager alarmManager;
public static void initAlarmManager(Context context){
Intent resultIntent = new Intent(context,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, 60 * 60 * 1000, pendingIntent);
}
My AlarmReceiver should now look for updates, and if there is new data of a certain condition, it should notify the user through a notification. This is part of my AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(Repository.ddfDb == null){
Repository.initDdfDb(context);
}
if(Repository.alarmManager == null){
Repository.initAlarmManager(context);
}
for(Episode episode : Repository.ddfDb.getListOfNextEpisodes()){
Notification.showNotification(context,episode);
}
}
}
Since my AlarmManager should start all the time , I let it start when the download is complete. To do this, I added the following to the manifest file:
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
initAlarmManager() onCreate() mainActivity. , , AlarmManager , . "" " ", AlarmManager , , .
initAlarmManager() AlarmReceiver. , , , , AlarmManager null . .
: " " , AlarmManager, . , , .
, , , , , . , . , WhattsApp, .
, AlarmManager , ?
!