Search code examples
androidservicealarmmanagerbootcompleted

Android BOOT_COMPLETED runs Service successfully but it opens the app too


In my application, I've alarm feature set on multiple time. So after device restart/start, I'm fetching alarm data from DB and set alarm again using Service. This works fine but after setting the alarm (I printed on log), app open as well. But I'm not starting any activity from that Service. Please have a look at the Service class code below:

public void onCreate() {
    super.onCreate();

    DBHelper moodsDB = new DBHelper(this);

    ArrayList<HashMap<String, String>> reminder_list = moodsDB.getAllReminders();

    for (HashMap<String, String> reminder : reminder_list) {
        if(Integer.parseInt(reminder.get("active").toString()) == 1){
            Date time = Utility.stringToDate(reminder.get("time").toString(), Utility.TIME_FORMAT_APP);
            Log.i("Mood Journal", "Reminder Time = " + Utility.dateToString(time, Utility.TIME_FORMAT_APP));

            Utility.cancelAlarmIfExists(this, Integer.parseInt(reminder.get("id").toString()));

            Utility.setAlarm(this, 
                    Integer.parseInt(reminder.get("id").toString()),
                    time.getTime());
        }
    }
}

What could be reason and how to solve this flaw, please suggest.


Solution

  • long now = new Date().getTime(); 
    while (time < now) { 
        time += AlarmManager.INTERVAL_DAY; 
    }
    

    I need to add the above code snippet before setting alarm to prevent app to open after device reboot.