What i'm trying to do should be pretty simple, i'm calling an IntentService to check periodically in background if the current time is before or after a specified time (passed as Extras in the intent from the MainActivity) and get a notification when it happens.
In Thread's run() method i get an instance of the current time and use it as comparison to the previously specified one in an if/else construct, before re-running the Thread. The problem is that it always go for "else" even if the current time is after the other one. This problem is just too strange, can anyone explain me where is the error?
Here is my IntentService class:
public class MyCheck extends IntentService {
int hour = 0; int minute = 0;
int i = 0;
private static final String TAG = "Check";
int hourNow;
int minuteNow;
Handler handler = new Handler();
public MyCheck() {
super(MyCheck.class.getName());
}
@Override
protected void onHandleIntent(Intent intent) {
Log.w(TAG, "Service Started!");
hour = intent.getIntExtra("hour", 0);
minute = intent.getIntExtra("minute", 0);
Log.w(TAG, "Step 1 ");
Thread runnable = new Thread() {
@Override
public void run() {
/* do what you need to do */
Log.w(TAG, "Step 2 ");
/* get current time */
Calendar c = Calendar.getInstance();
hourNow = c.get(Calendar.HOUR);
minuteNow = c.get(Calendar.MINUTE);
//HERE, IT SEEMS THAT IT'S ALWAYS BEFORE hour AND minute
if(hourNow > hour || (hourNow == hour && minuteNow > minute)) {
//SEND
Log.w(TAG, "RUN ");
Intent broadcast = new Intent();
broadcast.setAction(NOTIFICATION_SERVICE);
sendBroadcast(broadcast);
handler.removeCallbacks(this);
}
else {
//NOTHING
i++;
Log.w(TAG, "NOT YET " + i);
}
//REPEAT EVERY 6 SECONDS
try {
sleep(6000);
handler.post(this);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
runnable.start();
}
}
Aucun commentaire:
Enregistrer un commentaire