如何在我的例子中停止一个线程

时间:2017-09-26 13:14:41

标签: java multithreading

我有搜索问题(stackoverflow,谷歌),但我还没有得到任何好结果。我在Thread中写了一个简单的下载器。它(下载器)下载我想要的文件并保存到我的Android手机存储中。现在我想用我班上的方法停止下载。换句话说,我想停止下载线程。以下是我的代码。我现在该怎么办? 请指导我。最好的问候。

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();
        }

    }
}

1 个答案:

答案 0 :(得分:4)

thread.interrupt();

如果线程被中断,你必须检查旋转代码。例如

if( Thread.currentThread().isInterrupted()) return;