I am trying to write a service which runs as background process and
invoke my task (in application) at 9am everyday.
My service is:
public class DateService extends Service {
//expressed in milliseconds
private final long ONCE_PER_DAY = 1000 * 60 * 60 * 24;
private final int ONE_DAY = 1;
private final int NINE_AM = 9;
private final int ZERO_MINUTES = 0;
@Override
public void onCreate() {
Thread thr = new Thread(null, mTask, "DateService");
thr.start();
}
@Override
public void onDestroy() {
}
Runnable mTask = new Runnable() {
public void run() {
TimerTask myTask = new MyTask();
//perform the task once a day at 9 a.m., starting tomorrow
morning
Timer timer = new Timer();
timer.scheduleAtFixedRate(myTask, getTomorrowMorning9am(),
ONCE_PER_DAY);
// Done with our work... stop the service!
DateService.this.stopSelf();
}
};
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new Binder() {
@Override
protected boolean onTransact(int code, Parcel data, Parcel
reply,
int flags) throws RemoteException {
return super.onTransact(code, data, reply, flags);
}
};
private Date getTomorrowMorning9am() {
Calendar tomorrow = new GregorianCalendar();
tomorrow.add(Calendar.DATE, ONE_DAY);
Calendar result = new GregorianCalendar(
tomorrow.get(Calendar.YEAR),
tomorrow.get(Calendar.MONTH),
tomorrow.get(Calendar.DATE),
NINE_AM,
ZERO_MINUTES
);
return result.getTime();
}
}
Is this the right way to write my service? How would I verify my
service?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en