There is indeed a more efficient way to handle this.
Why don't you save the timestamp when the user closes the application and calculates the time elapsed after the user opens the application again? Then you can calculate the new food attribute that you made.
Edit: if you want to show notifications, if the food is low, you need an alarm manager.
Here is a sample code:
public static void registerAlarm(Context context) {
Intent i = new Intent(context, YOURBROADCASTRECIEVER.class);
PendingIntent sender = PendingIntent.getBroadcast(context,REQUEST_CODE, i, 0);
long startTime = SystemClock.elapsedRealtime();
startTime += 60000;
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, startTime, 900000, sender);
}
source
share