I suppose you see this when the device goes to sleep. Check out wake
locks to keep the CPU awake. Test this out thoroughly, nothing's more
annoying than a background thread run amok. Also note if you reach out
to the Internet to query a database server - Android shuts off the
WLAN adapter after 2mins or so. The phone may or may not have
connectivity once that occurs, depending whether a SIM card is in the
device, there's wireless connectivity etc.
Note the default connect and read timeouts are longer than 10s. Even
after dropping the connect and read TO's to align with your wait
times, dropping to 10s (endTime2) is pretty aggressive.
There's a lot of variables you need to consider and test out before
you can unleash this onto users.

On May 2, 6:24 am, guruk <[email protected]> wrote:
> hi,
> bellow just a test sample source for a background thread.
>
> i would like that when my app is closed it still run in the background
> (like at my tracks) to collect data even when it is closed.
>
> but it always dies after a while by itself, why?
>
> thanks
> chris
>
> package com.chris.alarmer;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.util.Log;
>
> public class alarmer extends Activity {
>     /** Called when the activity is first created. */
>
>         @Override
>     public void onDestroy() {
>         super.onDestroy();
>         Thread thr = new Thread(null, mTask, "alarmer");
>         thr.start();
>     }
>
>         @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>
>         Thread thr = new Thread(null, mTask, "alarmer");
>         thr.start();
>
>     }
>
>     Runnable mTask = new Runnable() {
>         public void run() {
>             // Normally we would do some work here...  for our sample,
> we will
>             // just sleep for 30 seconds.
>                 Log.i("DATABASE-TEST","-----------: START THREAD");
>                 long endTime = System.currentTimeMillis() + 99999*1000;
>                 long endTime2 = System.currentTimeMillis() + 10*1000;
>
>             while (System.currentTimeMillis() < endTime)
>             {
>                 //get data after 10 seconds
>                 if (System.currentTimeMillis() > endTime2)
>                 {
>                         endTime2 = System.currentTimeMillis() +10*1000;
>                         Log.i("DATABASE-TEST","-----------: GET DATA");
>                 }
>
>             }
>
>             Log.i("DATABASE-TEST","-----------: END THREAD");
>             // Done with our work...  stop the service!
>           //  alarmer.this.stopSelf();
>         }
>     };
>
> }
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to