Re: [android-developers] Writing a Date/Time Service

2010-12-31 Thread Kostya Vasilyev

31.12.2010 16:35, asinha пишет:

I am trying to write a service which runs as background process and
invoke my task (in application) at 9am everyday.

My service is:

[snip]

Is this the right way to write my service? How would I verify my
service?


This is error-prone, since a service can get kicked out of memory at any 
time, and all threads (including the thread used by Java timers) will 
terminate.


Set an Alarm with AlarmManager, it avoids the issues caused by 
interference from process lifecycles:


http://developer.android.com/reference/android/app/AlarmManager.html

--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Writing a Date/Time Service

2010-12-31 Thread TreKing
On Fri, Dec 31, 2010 at 7:35 AM, asinha  wrote:

> Is this the right way to write my service?
>

No. Use AlarmManager.


> How would I verify my service?
>

Test it?

-
TreKing  - Chicago
transit tracking app for Android-powered devices

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] Writing a Date/Time Service

2010-12-31 Thread asinha
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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en