I have been trying to build an app which checks for notification files every hour on a server. I have successfully implemented this using the alarm manager class, but the problem I am facing now is that the alarm does not get triggered on reboot, If the user does not launch the app after reboot. I want to automate this triggering of alarm. After searching for quite some time, I figured to launch an activity on reboot, which I know is bad practice, but is probably my only way out. I would like to check if there is some other way I could achieve what I want. I am open to not using alarm manager class for notifications. Please help with any suggestions.
This is my NotificationBootReceiver Class
package com.example.quickstart;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;
import java.io.IOException;
public class NotificationBootReceiver extends BroadcastReceiver {
String txt = "";
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Message1234","Boot Successfull ");
// Construct an intent that will execute the AlarmReceiver
Intent i = new Intent(context, MyAlarmReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(context, MyAlarmReceiver.REQUEST_CODE,
i, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every every half hour from this point onwards
long firstMillis = System.currentTimeMillis(); // alarm is set right away
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
// Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
2*60*60,pIntent);
// alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
// AlarmManager.INTERVAL_FIFTEEN_MINUTES, pIntent);
}
}
This is my android Manifest file receiver for NotificationBootReceiver.
<receiver android:name=".NotificationBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>