If you keep yourself running in the background forever, you are taking
those resources from the rest of the system forever.  This is not a
polite thing to do.  By using the alarm manager, the system has more
freedom to kill your process when it is not needed.

A service that keeps itself running forever but not actually doing
anything most of that time is a big waste of valuable resources, so
something to avoid.

And doing an HTTP operation every 5 seconds is WAY too much.  Keep in
mind, every line of code you run is lowering the level of the battery,
every network operation you do is lowering it even more.  Doing this
kind of thing every 5 minutes is the minimum scale you should be
thinking about.

On Jun 18, 11:00 pm, "Jaikishan Jalan" <[EMAIL PROTECTED]> wrote:
> Hi Zach,
>
> So, Once I receive the BOOT_COMPLETED_ACTION Intent, what is the problem if
> I start a thread which keeps running and sleep for time "t" and then again
> start running. Is this a bad approach ? Here is my code :
>
> This code will do some HTTP Post Request every 5 second. The idea is to run
> this in the background once my the emulator is started and keep this running
> until the mobile is switched off.
>
> import android.content.Context;
> import android.content.Intent;
> import android.content.IntentReceiver;
>
> public class AddressLogService extends IntentReceiver{
>   @Override
>   public void onReceiveIntent(Context context, Intent intent) {
>
>     Thread tr = new Thread(null,AddressTask,"Address Logger");
>     tr.start();
>   }
>
>   private Runnable AddressTask = new Runnable() {
>
>     public void run(){
>       while(true){
>
>         try{
>           //I do some HTTP Post Request.
>           Thread.sleep(5*1000);
>         }catch(Exception e){}
>       }
>
>     }
>
>   };
>
> }
>
> Thanks
> -J
>
>
>
> On Thu, Jun 19, 2008 at 8:09 AM, Zach Hobbs <[EMAIL PROTECTED]> wrote:
>
> > If you are doing periodic updates I would think about using the
> > AlarmManager
> > to kick off the process after specified intervals.  If you create a service
> > it's possible that it will be killed by the OS to free resources.
>
> >http://code.google.com/android/reference/android/app/AlarmManager.html
>
> > --
>
> > Zach Hobbs
> > HelloAndroid.com
> > Android OS news, tutorials, downloads
>
> > On Wednesday 18 June 2008 16:00:38 Jaikishan Jalan wrote:
> > > Hi,
>
> > > Thanks for the reply. I understood what you are saying. I wrote my Intent
> > > Receiver which will receive to this broadcast message. Now I want to run
> > a
> > > background process which keeps running forever ( which do some update
> > after
> > > every sometime t). My approach to this problem is this : I am going to
> > > write a Service Calls and call it from the onReceiveIntent method of the
> > > IntentReceiver I wrote. Now, onStart method of the Service Class, I will
> > > create a thread that updates and then sleep for time t and keep running.
>
> > > Is this a recommended way to approach this problem?
>
> > > Thanks
> > > J
>
> > > -----Original Message-----
> > > From: android-developers@googlegroups.com
> > > [mailto:[EMAIL PROTECTED] On Behalf Of Damien
> > > Sent: Wednesday, June 18, 2008 8:51 AM
> > > To: Android Developers
> > > Subject: [android-developers] Re: Running a program when emulator start
>
> > > Android broadcasts a message on boot. See
>
> >http://code.google.com/android/reference/android/content/Intent.html#...
> > >O MPLETED_ACTION
>
> > > If you have something listening for this broadcast then it will start
> > > as soon as the
> > > boot has completed.
>
> > > Regards
> > > D.
>
> > > On Jun 18, 11:18 am, Jaikishan <[EMAIL PROTECTED]> wrote:
> > > > Hi,
>
> > > > I was wondering if its possible to start a program automatically in
> > > > the background as soon as the emulator gets started? The idea is to
> > > > show some message on the status bar.
>
> > > > Thanks
> > > > -J
>
> <http://www.geocities.com/jai_ism>
--~--~---------~--~----~------------~-------~--~----~
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
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to