2

I have problems of searches(stackoverflow, google) and I didn't get any good results yet. I wrote a simple downloader within Thread. It (downloader) downloads my desired file and save into my android phone storage. Now I wanna stop the download with a method within my class. In other word I wanna stop downloader thread. Following is my code. How can I do this now? Please guide me. Best regards.

package com.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

    Thread   thread;

    Handler  handler = new Handler();

    TextView txt;

    boolean  first   = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button) findViewById(R.id.button1);
        txt = (TextView) findViewById(R.id.textView1);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (first) {
                    Downloader downloader = new Downloader("http://192.168.43.131/and/filmLow3.mp4");
                    first = false;
                } else {
                    if ( !thread.currentThread().isInterrupted()) {
                        thread.interrupt();
                    }
                }
            }
        });

    }


    public class Downloader {

        int downloaded = 0;


        public Downloader(final String link) {

            thread = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        URL url = new URL(link);
                        HttpURLConnection connection;
                        connection = (HttpURLConnection) url.openConnection();
                        connection.connect();

                        final int size = connection.getContentLength();

                        InputStream inputStream = connection.getInputStream();

                        FileOutputStream outputStream = new FileOutputStream(Base.appPath + "file.mp4");

                        byte[] buffer = new byte[8 * 1024];
                        int len = 0;

                        while ((len = inputStream.read(buffer)) != -1) {
                            downloaded += len;
                            outputStream.write(buffer, 0, len);
                            handler.post(new Runnable() {

                                @Override
                                public void run() {
                                    txt.setText(downloaded / 1024 / 1024 + " MB || " + size / 1024 / 1024 + " MB");

                                }
                            });
                        }
                        inputStream.close();
                        outputStream.close();

                    }
                    catch (IOException e) {

                        e.printStackTrace();
                    }

                }
            });
            thread.start();
        }

    }
}
3

1 Answer 1

4
thread.interrupt();

And you have to check in spinning code if thread was interrupted. For example

if( Thread.currentThread().isInterrupted()) return;
5
  • 1
    this is the way you should stop the thread from another one. Post code with your change, and we will see what should be changed. Also note that you cannot stop a thread immidiately. You can just send a signal, and thread should look for it. Sometimes libraries throw InterruptedException, but not always
    – mlecz
    Sep 26, 2017 at 13:37
  • I did not know that I should not stop the Thread immediately. OK. My code is above. Please suggest the right way.
    – Hadi
    Sep 26, 2017 at 13:56
  • In onClick, else statement should be just thread.interrupt(). And in download thread in while loop there should be if( Thread.currentThread().isInterrupted(){close all streams and; return; }
    – mlecz
    Sep 26, 2017 at 14:04
  • 1
    easiest would be to make the while condition check the interupt flag : while (!Thread.currentThread().isInterrupted() && (len = inputStream.read(buffer)) != -1), this will exit the while loop and closing the streams is already in place
    – bowmore
    Sep 26, 2017 at 14:06
  • Thank you my friend. your code worked. thanks again):
    – Hadi
    Sep 26, 2017 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.