0

I have created an app, that allows a user to input assignment deadlines and then a time to be notified that the assignment is due, this all works fine, however, I can't find a clear example of how to make the alarms survive a reboot.
The database stuff on the receiver is just to populate the notification with the specific assignments information. The alarms themselves are not saved in the database, the times and dates are set by user input and directly passed to alarm manager.
Alarm creation page:

Long nIdLong = System.currentTimeMillis();
            String nId = nIdLong.toString();
    final int _id = (int) System.currentTimeMillis();

                Intent alarmIntent = new Intent(context, AlarmReceiver.class);
                alarmIntent.putExtra("nID", nIdLong);
                // make sure the intent have id's
                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, _id,  alarmIntent , 0);

                AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
                alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

This is my receiver:

public void onReceive(Context context, Intent intent)
    {
        long dbId = intent.getLongExtra("nID", 0);
        String notificationId = Long.toString(dbId);


        Log.i("App", "called receiver method");
        try{

            userDbHelper = new UserDbHelper(context.getApplicationContext());
            sqLiteDatabase = userDbHelper.getReadableDatabase();
            cursor = userDbHelper.getAssignmentNotification(notificationId, sqLiteDatabase);
            if (cursor.moveToFirst()) {
                do {

                    id = cursor.getString(0);
                    name = cursor.getString(1);
                    subject = cursor.getString(2);
                    date = cursor.getString(3);
                    time = cursor.getString(4);

                } while (cursor.moveToNext());
            }
            NotificationGenerator.generateNotification(context, date, time, name, subject);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
user6548194
  • 45
  • 1
  • 10

1 Answers1

0

https://examples.javacodegeeks.com/android/core/activity/android-start-service-boot-example/ This example shows how to start services on boot, save the alarms into a database and have the on boot service run through all the saved alarms to have them recreated upon each boot.

lmprowse
  • 113
  • 6