Reputation: 11
I'm not sure how to declare to destroy this activity. If I exit the application it crashes the whole phone because it's running in the background. I had an outline I was given in another question, but I'm ot sure how to implement that. I currently have:
new Thread(new Runnable(){
@Override
public void run() {
while(true)
handler.sendEmptyMessage(FIRE);
}
}).start();
So how do I stop this. I cannot stop a thread, I'm reading that's dangerous. So how should I structure this so it's not crashing the system? Should I remove the while
loop, make it a method, and kill the method at pause? Like:
public void run() {
handlerMsg();
}}).start():
Then I could do:
onFinish() {
handlerMsg.stop
}
That feels wrong. Any assistance would be met with much gratitude.
Upvotes: 0
Views: 1503
Reputation: 10479
Generally speaking, there are several mechanisms for doing what you are attempting to doing, with varying levels of complexity and effectiveness. I'd like to broadly describe a few.
Services
Services can be thought of as headless Activities. The idea is that a Service publishes an interface through which other code can interact with it. A service is managed by Android, started when needed, shutdown when not.
A service provides some unique qualities: (From linked doc)
So, you'll want to use a service when you want some processing done in a fashion that allows it to be continued after the activity is stopped or you'd like to share a single instance of the processing code between multiple apps.
In my opinion, this comes with some complexity. Simple Services are, by design simple. Complex services are very complex, requiring lots of design effort. On top of this, Services are something that tend to be 'nuked' by poorly informed users with TaskKillers.
HandlerThread
The second option is a HandlerThread. I find this to be a bit less complex in terms of both application construction and maintenance. Handlers use a system of messages and queues to communicate. This creates a nice balance, in my opinion, between code complexity and the ability to decouple a private worker service from other components of the app. You would need to manage the life cycle of this HandlerThread by calling start() on the HandlerThread from an owning class and then sending it a message you define to be the shutdown message and having it stop itself. You can find an example of the configuration here
Runnable
The classic example of how to do bare bones multi threading. You've already got and idea for how to do this. The best example of a way to stop a single thread is setting your own environment variable to be monitored by the thread.
private static boolean stop = false;
new Thread(new Runnable {
public void run(){
if(!stop){
//do work
}
}
});
Thread.sleep(1000);
stop = true;
You can find a good discussion on this topic here
Based on the stage it looks like you are at, you can certainly consider switching to one of the other more robust threading options based on your requirements.
Upvotes: 2
Reputation: 1279
If you are using the Thread class for a very longrunning (always running) thing without a framework around (see: ThreadExecutor), eventually processing messages, you misunderstood its intented usage. This should be done in a Service. If you want to trigger something each now and then, you probably can also get around with a Handler (which can be set to fire after some time, and then reset to fire again in later time).
Back to your basic question: You are always free to stop a running Thread Object, as long as the thread itself is capable of stopping itself. Check out the interrupt() and interrupted() functions of the Thread class. You post a Thread.interrupt() to your thread, the thread has to check (Exception), if something wants him to stop and do as requested. Anyways, you'll be better working with Service. It will start as soon as your Activity(s) binds to it and automatically stop as soon as it has no binding anymore.
Upvotes: 2
Reputation: 13541
Why dont you just add a simple condition that updates a global variable that the thread looks at and when you want it to stop, then have the thread reach its end.
like this:
thread t = new thread(new runnable(){
public void run()
{
while(continue())
{
handler.send(....)
}
}
private bool continue()
{
doing something
return false when done
else true
}
doing it this way, will allow you to control when the thread stops (reaches its end) instead of forcibly stopping it.
Also another thing to consider is if its an activity with no UI you might consider to make it into a service instead of a fully fledged activity. This allows it to run in the background and do what it needs to do without any user interaction!
I hope this helps.
Flow of it would be
Activity launches service -> Service spawns thread -> does its work -> returns the ok via a broadcast, then stop the service.
If this helps, don't forget to mark it with an answer :)!
Upvotes: 0