activity 启动一个service服务,service启动多线程下载,OK都好着;
但是出了一个奇怪的问题:
下载过程中,点击BACK键 退出应用,一次好着;
重复进入应用,退出应用;
怪事出了,
下载停了:
log 如下:09-15 16:04:53.288: INFO/downing(10770): len is...1024
09-15 16:04:53.288: INFO/downing(10770): len is...1024
09-15 16:04:53.288: INFO/downing(10770): len is...1024
09-15 16:04:53.288: INFO/downing(10770): len is...640
09-15 16:04:53.303: INFO/Process(1385): Sending signal. PID: 10770 SIG: 9
09-15 16:04:53.335: INFO/WindowManager(1385): WIN DEATH: Window{45f17de0 SurfaceView paused=false}
09-15 16:04:53.342: INFO/WindowManager(1385): WIN DEATH: Window{45e9a800 com/.Camera_mainActivity paused=false}
09-15 16:04:53.342: INFO/WindowManager(1385): WIN DEATH: Window{45faaa60 Toast paused=false}
09-15 16:04:53.764: DEBUG/dalvikvm(1385): GC_EXPLICIT freed 3431 objects / 163496 bytes in 191ms
09-15 16:05:00.014: DEBUG/KeyguardUpdateMonitor(1385): received broadcast android.intent.action.TIME_TICK
09-15 16:05:00.022: DEBUG/KeyguardUpdateMonitor(1385): handleTimeUpdate
09-15 16:05:02.041: WARN/ActivityManager(1385): Activity destroy timeout for HistoryRecord{45e1ab90 com/.Camera_mainActivity}

解决方案 »

  1.   

    下载 代码:
    /**
     * @author ideasandroid 主下载线程
     */
    public class downloadTask extends Thread 
    {
    private int blockSize, downloadSizeMore;
    private int threadNum = 1;
    String urlStr, threadNo, fileName; public downloadTask(String urlStr, int threadNum, String fileName) 
    {
    this.urlStr = urlStr;
    this.threadNum = threadNum;
    this.fileName = fileName;
    FileMultyThread.isInterrupt = false;
    } @Override
    public void run() {
    fds = new FileMultyThread[threadNum];
    try {
    URL url = new URL(urlStr);
    URLConnection conn = url.openConnection();
    // 获取下载文件的总大小
    fileSize = conn.getContentLength(); if (fileSize < 0) {
    checkNetHandler.sendEmptyMessage(0);
    return;
    } HandleImage.nNotification = true;
    showNotification();
    // 计算每个线程要下载的数据量
    blockSize = fileSize / threadNum;
    // 解决整除后百分比计算误差
    downloadSizeMore = (fileSize % threadNum);
    File file = new File(fileName); for (int i = 0; i < threadNum; i++) {
    // 启动线程,分别下载自己需要下载的部分
    int offset = (i + 1) * blockSize - 1;
    if (i == (threadNum - 1)) {
    offset += (downloadSizeMore);
    }
    FileMultyThread fdt = new FileMultyThread(url, file, i
    * blockSize, offset);
    fdt.setName("Thread" + i);
    fdt.start();
    fds[i] = fdt;
    } boolean finished = false;
    int length = fds.length;
    while (!finished && nRun) {
    finished = true;
    for (int i = 0; i < length; i++) {
    if (!fds[i].isFinished()) {
    finished = false;
    }
    }
    }
    Log.i("ddd","download file size =" + file.length());
    // ...

    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }/**
     * 多线程 分段下载
     * 
     * @author jinxiaofeng 2011-7-27
     */
    public class FileMultyThread extends Thread 
    {
    private static final int BUFFER_SIZE = 1024;
    private URL url;
    private File file;
    private int startPosition;
    private int endPosition;
    private int curPosition;
    // 用于标识当前线程是否下载完成
    private boolean finished = false;
    private int downloadSize = 0;
    public static boolean thPause=true;
    public static Object obj = new Object();
    // private CheckLeme mCheckLeme;
    // RemoteViews notificationView; // 是否中断
    public static boolean isInterrupt = false; /**
     * 构造函数
     * 
     * @param url
     * @param file
     * @param startPosition
     * @param endPosition
     * @param dialog
     */
    public FileMultyThread(URL url, File file, int startPosition,
    int endPosition) {
    this.url = url;
    this.file = file;
    this.startPosition = startPosition;
    this.curPosition = startPosition;
    this.endPosition = endPosition;
    } @Override
    public void run() 
    {
    BufferedInputStream bis = null;
    RandomAccessFile fos = null;
    byte[] buf = new byte[BUFFER_SIZE];
    URLConnection con = null;
    try 
    {
    //URL url = new URL(strURL);
    con = url.openConnection();
    // 获取下载文件的总大小
    //CheckLeme.fileSize = con.getContentLength();
    con.setAllowUserInteraction(true);
    // 设置当前线程下载的起点,终点
    con.setRequestProperty("Range", "bytes=" + startPosition + "-" + endPosition);
    // 使用java中的RandomAccessFile 对文件进行随机读写操作
    fos = new RandomAccessFile(file, "rw");
    // 设置开始写文件的位置
    fos.seek(startPosition);
    bis = new BufferedInputStream(con.getInputStream());
    // 开始循环以流的形式读写文件
    while (curPosition <= endPosition && !isInterrupt) 
    {
    // 每次写文件的长度
    int dataSize = endPosition - curPosition + 1;
    int len = 0;
    if (dataSize < BUFFER_SIZE) {
    len = bis.read(buf, 0, dataSize);
    } else {
    len = bis.read(buf, 0, BUFFER_SIZE);
    }
    Log.i("downing", "len is..." + len);
    thPause = false;
    if (len == -1) {
    len = 0;
    Thread.sleep(15000);
    }
    fos.write(buf, 0, len);
    curPosition = curPosition + len;
    synchronized (obj) 
    {
    CheckLeme.havaDownloadSize += len;
    }
    }
    bis.close();
    fos.close();
    if (!isInterrupt) {
    this.finished = true;
    Thread.currentThread().join();
    Log.i("jim", "Thread " + Thread.currentThread().getName()
    + " finished ");
    } else {
    return;
    }

    }
    catch (IOException e) 
    {
    Log.d(getName() + " Error:", e.getMessage());

    catch (InterruptedException e) 
    {
    e.printStackTrace();
    } finally {
    }
    } public boolean isFinished() 
    {
    return finished;
    } public int getDownloadSize() 
    {
    return downloadSize;
    } @SuppressWarnings("static-access")
    public void setIsInterrupt(boolean isInterupt) 
    {
    this.isInterrupt = isInterupt;
    }}