So I have modified my thread to be a singleton and mimplemented it
like this. Hope it helps someone else who might be facing the same
issue.
public class SingletonThread extends Thread{
private static SingletonThread _instance;
public static boolean IS_RUNNING = false;
private static Context mContext;
private SingletonThread(){
}
public synchronized static final SingletonThread getInstance(Context
context){
mContext = context;
if(instance == null){
instance = new SingletonThread();
}
return instance;
}
public synchronized void run(){
IS_RUNNING = true;
//........Implement your requirements.
IS_RUNNING = false;
}
}
And whenever I call the getInstance, from any other UI thread, I will
check for IS_RUNNING also and will start the thread only if IS_RUNNING
is false/will wait till IS_RUNNING is false.
There might be better ways to do this, so please post your feedbacks,
Thank you.
On Mar 11, 2:44 pm, Dianne Hackborn <[email protected]> wrote:
> On Thu, Mar 11, 2010 at 1:40 PM, Achanta <[email protected]> wrote:
> > Yes I am trying to redesign it to use AsyncTask. One reason why I did
> > not take this approach is that I just wanted a plain thread that sits
> > and logs user events that are occurring throughout my app and it made
> > more sense to just start that thread when the app starts and shut it
> > down when the app closes. I had a handler which grabs the logs and
> > puts them in db and the thread just sits there and logs them to
> > server.
>
> For this model, manage the thread through a separate static singleton, have
> clients tell it when they are using it (in onCreate() or whatever) and when
> they are done (in onDestroy() or whatever), and have that class take care of
> stopping the thread when there are no more clients.
>
> Also please be careful about what this thread is doing. An application
> should be careful to be doing very little work any time it is not in the
> foreground, or it will be consuming battery... and has a good chance of
> showing up high up in the battery meter as it eats the user's battery.
>
> --
> Dianne Hackborn
> Android framework engineer
> [email protected]
>
> Note: please don't send private questions to me, as I don't have time to
> provide private support, and so won't reply to such e-mails. All such
> questions should be posted on public forums, where I and others can see and
> answer them.
--
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