patternjavaMinor
App to give cigarettes through the day
Viewed 0 times
theappcigarettesgivethroughday
Problem
I have this app, that gives you cigarettes through the day, with equal intervals from one another.
These are some important variables:
All this is happening in a
I want you to please look my code, and tell me if it looks like there are no errors, because my client is reporting, that on the second day, there are errors, giving many cigarettes at once.
Here is the
This is in the
```
private void cigaretteCheck() {
try {
Thread.sleep(1000);
if (MyPrefs.isGivingCigarettes() && nextCigaretteTime = (cigarettesSmokedToday + cigarettesForToday)) {
MyPrefs.setGivingCigarettes(false, this);
}
}
if (System.currentTimeMillis() >= (wakeUp + Constants.ONE_DAY)) {
initVariables();
if (MyPrefs.isExtraCigar
These are some important variables:
wakeUp- is the time, when you wake up in the day, and if the user has specified that he sleeps 8 hours, than the wake hours are 16, and the cigarettes must be distributed evenly for those hours.
oneCigaretteTime- calculated based on how many hours the user is awake. For example, the user has 10 cigarettes for the first day, and 10 cigarettes for the second day, if he doesn't smoke them, they are given to the next day, so then the app must calculate time intervals with 20 cigarettes.
All this is happening in a
ForegroundService within a constantly running thread, with Thread.sleep(1000) and then an if check.I want you to please look my code, and tell me if it looks like there are no errors, because my client is reporting, that on the second day, there are errors, giving many cigarettes at once.
Here is the
initialize() method that is also being started inside onStartCommand of service:private void initialize() {
initVariables();
nextCigaretteTime = wakeUp + oneCigaretteTime;
startForeground(Constants.FOREGROUND_ID, ForegroundService.buildForeground(this, allowedCigarette));
broadcaster.sendBroadcast(new Intent(Constants.CIG_INTENT));
threadRunning = true;
myThread = new Thread() {
@Override
public void run() {
while (threadRunning) {
cigaretteCheck();
}
}
};
myThread.start();
}This is in the
Thread:```
private void cigaretteCheck() {
try {
Thread.sleep(1000);
if (MyPrefs.isGivingCigarettes() && nextCigaretteTime = (cigarettesSmokedToday + cigarettesForToday)) {
MyPrefs.setGivingCigarettes(false, this);
}
}
if (System.currentTimeMillis() >= (wakeUp + Constants.ONE_DAY)) {
initVariables();
if (MyPrefs.isExtraCigar
Solution
I was being somewhat serious when I said that checking once a second is a sign of extreme addiction. On a mobile device, where every computation uses battery power, I think that it would be a worthwhile tradeoff to sleep for 30 or even 60 seconds between checks. That means that your app would wake up 30 to 60 times less frequently — a significant savings over time.
Better yet, you shouldn't be polling at all. All the times are known in advance, so schedule them using the
Better yet, you shouldn't be polling at all. All the times are known in advance, so schedule them using the
Timer API. When conditions change, then cancel and reschedule all the outstanding timer tasks. That makes the operating system do all the hard work for you.Context
StackExchange Code Review Q#69474, answer score: 4
Revisions (0)
No revisions yet.