x

public class MyInfo extends Activity{
    private ArrayList<MainInfoBean> m_beanList = new ArrayList<MainInfoBean>();
    private  AQuery aq;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myinfo);
        aq = new AQuery(this);

        final ArrayList<FileDownload> filedownloadList = new ArrayList<FileDownload>();

       Thread myThread =  new Thread(new Runnable() {
            @Override
            public void run() {
                FileDownload fd = new FileDownload();
                filedownloadList.add(fd);
                Log.e("H3c","-----1------"+filedownloadList.size());
                try {
                    fd.init("http://gdown.baidu.com/data/wisegame/c5205df8a4e6cc3a/renren_5090400.apk", "/mnt/sdcard","h3cTest.apk","123");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

        Thread myThread2 =  new Thread(new Runnable() {
            @Override
            public void run() {
                FileDownload fd = new FileDownload();
                filedownloadList.add(fd);
                Log.e("H3c","-----2------"+filedownloadList.size());
                try {
                    fd.init("http://gdown.baidu.com/data/wisegame/c5205df8a4e6cc3a/renren_5090400.apk", "/mnt/sdcard","h3cTest2.apk","1234");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

        DownloadManager dm = DownloadManager.getInstance();
        dm.Init();
        dm.addDownloadTask(myThread);
        dm.addDownloadTask(myThread2);

        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        while (true){
                        Log.e("H3c",filedownloadList.size()+"-=-=");
                        for(FileDownload ffd :filedownloadList)
                        {
                            Log.e("H3c",ffd.getProgress()+"=");
                        }
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        }
                    }
                }
        ).start();


//        initData();
//        initView();
    }

    public void initData(){
        m_beanList.clear();

        for (int n  = 0;n < 45;n++) {
            MainInfoBean tmpBean = new MainInfoBean();
            tmpBean.setMainTitle("我的第"+n+"幅作品");
            tmpBean.setMainAuthor("H3c");
            m_beanList.add(tmpBean);
        }

        String url = "http://112.65.187.114:8808/eia/ieia?method=getPoint&sessionId=EEF483F9045FF6DBE90B37CEE93C46C4";
        aq.ajax(url, JSONObject.class, this, "jsonCallback");

        aq.ajax(url, String.class, new AjaxCallback<String>() {

            @Override
            public void callback(String url, String html, AjaxStatus status) {
                Log.e("H3c","web:"+html);
            }

        });
    }

    public void jsonCallback(String url, JSONObject json, AjaxStatus status){

        if(json != null){
            //successful ajax call
            Log.e("H3c",json.toString());
        }else{
            //ajax error
            Log.e("H3c","error:"+status.getCode());
            Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
        }

    }

    public void initView(){
        aq.id(R.id.main_listview).adapter(new MainListAdapter(m_beanList,this));
        aq.id(R.id.main_listview).scrolled(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int i) {
                // 判断是否滚动到底部
                boolean scrollEnd = false;
                try {
                    if (view.getPositionForView(aq.id(R.id.main_listview).getListView()) == view
                            .getLastVisiblePosition())
                        scrollEnd = true;
                } catch (Exception e) {
                    scrollEnd = false;
                }

                Log.e("H3c",scrollEnd+"===="+view.getPositionForView(aq.id(R.id.main_listview).getListView())+"----"+ view
                        .getLastVisiblePosition());
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i2, int i3) {

            }
        });
    }
}

 

package com.h3c.helenstudio.download;

import android.util.Log;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by Administrator on 13-6-11.
 */
public class FileDownload {
    private String m_fileUri;
    private int m_fileSize;
    private float downLoadFilePosition;//文件下载进度
    private String m_fileName;
    private String m_filePath;
    private String m_TAG;

    public int getProgress() {
        if (m_fileSize == 0)
        {
            return 0;
        }
        return (int)(downLoadFilePosition / m_fileSize * 100);
    }

    public boolean start() {
        return false;
    }

    public boolean pause() {
        return false;
    }

    public boolean stop() {
        return false;
    }

    public void setFileDownloadUrl(String uri) {
        this.m_fileUri = uri;
    }

    public void setFileName(String fileName) {
        this.m_fileName = fileName;
    }

    public void init(String url, String filePath, String fileName, String fileTag) throws IOException {
        if (fileName == null || fileName == "") {
            this.m_fileName = url.substring(url.lastIndexOf("/") + 1);
        } else {
            this.m_fileName = fileName;
        }
        this.m_filePath = filePath;
        this.m_fileUri = url;
        URL Url = new URL(url);
        URLConnection conn = Url.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();

        this.m_fileSize = conn.getContentLength();// 根据响应获取文件大小
        if (this.m_fileSize <= 0) { // 获取内容长度为0
            throw new RuntimeException("无法获知文件大小 ");
        }
        if (is == null) { // 没有下载流
//            sendMsg(Down_ERROR);
            throw new RuntimeException("无法获取文件");
        }

        FileOutputStream FOS = new FileOutputStream(this.m_filePath +"/"+ this.m_fileName); // 创建写入文件内存流,通过此流向目标写文件

        byte buf[] = new byte[2048];
        downLoadFilePosition = 0;
        Log.e("H3c","start");
        int numRead;
        while ((numRead = is.read(buf)) != -1) {
            FOS.write(buf, 0, numRead);
            downLoadFilePosition += numRead;
//            Log.e("H3c",Thread.currentThread().getId()+"read:"+downLoadFilePosition);
        }
        try {
            is.close();
            FOS.close();
        } catch (Exception ex) {

        }
        Log.e("H3c","success");

    }

    public void setTag(String tag) {
        this.m_TAG = tag;
    }

    public void setFilePath(String filePath) {
        this.m_filePath = filePath;
    }
}

 

package com.h3c.helenstudio.download;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 13-6-11.
 */
public class DownloadManager {
    ExecutorService pool;
    private static DownloadManager instance=null;

    public static DownloadManager getInstance() {
        if(instance==null)
            instance=new DownloadManager();
        return instance;
    }

    public void addDownloadTask(Thread thread)
    {
        pool.submit(thread);              //加入线程
    }

    public void Init()
    {
        pool = Executors.newFixedThreadPool(3);

//        pool.execute(new Thread(){              // 所有线程结束之后执行
//            public void run()
//            {
//                pool.execute(new Thread(){
//                    public void run()
//                    {
//
//                    }
//                });
//            }
//        });
    }
}



posted @ 2013-06-12 17:20  爱生活,爱编程  阅读(220)  评论(0编辑  收藏  举报