For a 30-minute interval, I'd recommend an alarm manager. It'll save
some RAM that'll make the device more responsive for foreground
applications, and it'll save you some bookkeeping (tracking a 30-
minute interval through your service getting killed and restarted
isn't as easy as it seems).

As for the apparently random behavior, I don't actually know for sure.
My guess is that the system is running under enough memory pressure
(which is common during boot as there are many things going on) to
cause apparently inactive processes to get killed so quickly that your
thread never gets a chance to run at all.

JBQ

On Jul 13, 10:43 am, "Jaikishan Jalan" <[EMAIL PROTECTED]> wrote:
> Thanks Mark and JBQ for your reply. Ok, ideally when the application will be
> released, my application will make a network request after every 30 minutes.
> So what do you recommend? - Service or Alarm Manager? My second question is
> why the random behavior? Is there any reason behind why the thread sometime
> runs and sometime it does not.
>
> -Jaikishan
>
> -----Original Message-----
> From: [email protected]
>
> [mailto:[EMAIL PROTECTED] On Behalf Of JBQ
> Sent: Sunday, July 13, 2008 10:39 AM
> To: Android Developers
> Subject: [android-developers] Re: Running Background Process
>
> First, I'm not entirely sure what you're trying to achieve, but making
> your code wake up every 15 seconds might cost your users a lot of
> battery life for little result, when the user isn't actively using the
> device. If you do some network activity every 15 seconds over a cell
> network, you'll only have a few hours of battery life.
>
> You really have two options here:
>
> -if you really want to have a process stick around, you need to have a
> Service in that process, so that the system can make the appropriate
> decisions when it tries to make space for other applications. When the
> process that hosts your service is killed, it'll eventually get
> restarted automatically, so you can restart whatever you were doing in
> the service's onCreate function. In that case, you'll find that using
> a Handler is a much better idea than using Thread.sleep.
>
> -if you can work without necessarily having a process stay up all the
> time, you should look at the AlarmManager, which will allow the system
> to free the memory associated with your process between ticks.
>
> JBQ
>
> On Jul 13, 7:50 am, "Jaikishan Jalan" <[EMAIL PROTECTED]> wrote:
> > Hello,
>
> > I have written an intentreceiver which gets launched when the boot has
> > completed. In this, I run a thread in the background process which do
> > certain task periodically. Here is my piece of code:
>
> > public class AddressLogService extends IntentReceiver{
> >   /* the intent source*/
> >   static final String ACTION =
> > "android.intent.action.BOOT_COMPLETED";
> >   @Override
> >   public void onReceiveIntent(Context context, Intent intent) {
> >     if (intent.getAction().equals(ACTION))
> >     {
> >       NewRunnable AddressLog = new NewRunnable(context);
> >       Thread tr = new Thread(null,AddressLog,"Address Logger");
> >       tr.start();
> >     }
> >   }
>
> >   private class NewRunnable implements Runnable {
> >     private Context context;
> >     // Initializing the Runnable Class
> >     public NewRunnable(Context c){
> >       context = c;
> >     }
> >     public void run(){
> >       while(true){
> >          isServerUp = true;
> >          if(isServerUp ){
> >             // Do some processing
> >             Thread.sleep(15 * 1000);
> >           }
> >         }catch(Exception e){}
> >       }
> >     }
> >   }
>
> > }
>
> > I have included the required receiver details and permission in
> > AndroidManifest.xml
> > The problem is when I launch my emulator, this thread sometimes run and
> > sometime it does not run. I have to try launching the emulator certain
> times
> > before I get this thread running. I check this by checking the server log.
> I
> > am not sure why this is happening. Can any point me out why this is
> > happening? Or is anyone can suggest there is any other better way to
> > approach this task.
>
> > Thanks,
> > Jaikishan
--~--~---------~--~----~------------~-------~--~----~
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]
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