0

My application has one service which runs until user pressed exit button. I know giving exit button in android is not good design but in my application it is desired.

In my application I also have a thread to send Http request, download a file and then parse it in background so that UI will not block. In my thread's run method there are sequential steps (like download a file, then parse it), there is no while or for loop in it.

When user presses exit button, is there any way to stop the background thread if it is running without kill process (using Process.kiil(pid) or System.exit(0)) ?

I have tried AsyncTask also. Whatever in run method of thread, i put it in doInBackground method. When user presses exit button i have canceled asynctask. Cancelling task not stop the background thread completely because after file download it will go in parser to parse the file (i.e. parsing is done in background thread but it at the time of parsing it is not in doInBackground or run method. It will in parsing method of parser class or in Default handler class for parsing.)

From googling I read many blogs and other stackoverflow questions about thread but still I cannot find any solution for it. Please help....thanks in advance..

Khushbu Shah
  • 1,603
  • 3
  • 27
  • 45

4 Answers4

0

Stop thread at instance (when exit event occurred from UI) is not possible without using

Process.killProcess(Process.myPid());
Khushbu Shah
  • 1,603
  • 3
  • 27
  • 45
0

before each step you could add a line like

if(stillRunning){ 
//next step
}

and when you want it to stop, you just set stillRunning to false.

CAA
  • 968
  • 10
  • 27
  • But how it is possible on every step of xml parser (subclass of Default handler like [this](http://www.ibm.com/developerworks/opensource/library/x-android/index.html) ( Listing 6. The SAX handler) – Khushbu Shah Mar 28 '12 at 07:03
0

(1) use this to stop application instead of exit

            finish();
    Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.addCategory(Intent.CATEGORY_HOME); 
    startActivity(intent);

(2) use new Thread instead of asynk task which you can stop by calling stop method

            Thread myThread = new Thread ()
        {
            public void run()
            {
                Looper.prepare();
                doMyWork();
            }


        }.start();

and call stop on myThread

vipin
  • 2,851
  • 4
  • 18
  • 32
  • but be carefull when using stop method read this http://stackoverflow.com/questions/680180/where-to-stop-destroy-threads-in-android-service-class and also this http://stackoverflow.com/questions/94011/how-to-abort-a-thread-in-a-fast-and-clean-way-in-java – vipin Mar 28 '12 at 07:05
  • for first option it will not stop background thread. it will finish only activity. and for second option stop() method of thread is deprycated. – Khushbu Shah Mar 28 '12 at 07:06
  • first one is not for stopping your thread it is for you are using exit which is not a good practice use that at the place of exit where you want to close application – vipin Mar 28 '12 at 07:08
  • I know to give exit is not good practice but it is desire in my application. I want a solution which stops the background thread. – Khushbu Shah Mar 28 '12 at 07:11
  • please check my first comment there i have given two links – vipin Mar 28 '12 at 07:12
0

Probably, your connection and parser use same InputStream. If you quit, try to close the stream and a connection. In that case, some kind of Exception will be thrown and background thread will finish job with error but very fast. If you are in the middle of file saving, you must check every write(buffer) if you are canceled or try to close FileOutputStream.

Damian Kołakowski
  • 2,731
  • 22
  • 25