文件下载

public class SyncThread implements Runnable {
    private static final String TAG = "SyncThread";
    private Context                    mContext;
    private String                     mUrl;
    private String                     mFilename;
    private NotificationManager        mNotifyManager;
    private NotificationCompat.Builder mBuilder;

    private SyncThread(Context ctx, String Url, String filename) {
        this.mContext = ctx;
        mUrl = Url;
        mFilename = filename;
        mNotifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(mContext);
    }


    public static SyncThread getInstace(Context ctx, String url, String filename) {
        return new SyncThread(ctx, url, filename);
    }


    @Override
    public void run() {
        //下载文件
        downLoad(mUrl, mFilename);
    }

    private void updateProgress(int progress) {
        mBuilder.setContentText("下载进度" + progress + "%").setProgress(100, progress, false);
        PendingIntent pendingintent = PendingIntent.getActivity(mContext, 0, new Intent(), PendingIntent.FLAG_CANCEL_CURRENT);
        mBuilder.setContentIntent(pendingintent);
        mNotifyManager.notify(0, mBuilder.build());
    }

    private synchronized void downLoad(String murl, String filename) {
        InputStream in = null;
        FileOutputStream out = null;
        int oldProgress = 0;
        try {
            URL url = new URL(murl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setConnectTimeout(10 * 1000);
            urlConnection.setReadTimeout(10 * 1000);
            urlConnection.setRequestProperty("Connection", "Keep-Alive");
            urlConnection.setRequestProperty("Accept-Language", "zh-CN");
            urlConnection.setRequestProperty("Charset", "UTF-8");
            //urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
            urlConnection.setUseCaches(false);
            urlConnection.connect();
            long bytetotal = urlConnection.getContentLength();
            long bytesum = 0;
            int byteread = 0;
            in = urlConnection.getInputStream();
            File apkFile = new File(ADConstants.getIntance().getDownloadFileDir(mContext), filename);
            out = new FileOutputStream(apkFile);
            byte[] buffer = new byte[1024];
            while ((byteread = in.read(buffer)) != -1) {
                bytesum += byteread;
                out.write(buffer, 0, byteread);
                int progress = (int) (bytesum * 100L / bytetotal);
                if (progress != oldProgress) {
                    updateProgress(progress);
                }
                oldProgress = progress;
            }
            mBuilder.setContentText("下载完毕").setProgress(0, 0, false);
            Log.d(TAG, "下载完毕。。。");
            mBuilder.setContentText("文件检验成功");
            Notification noti = mBuilder.build();
            noti.flags = Notification.FLAG_AUTO_CANCEL;
            mNotifyManager.notify(0, noti);
            mNotifyManager.cancel(0);
        } catch (Exception e) {
            mBuilder.setContentText("下载失败:" + e.getMessage());
            Log.d(TAG, "下载失败。。。:" + e.getMessage());
            Notification noti = mBuilder.build();
            noti.flags = Notification.FLAG_AUTO_CANCEL;
            mNotifyManager.notify(0, noti);
        } finally {
            StreamUtils.closeStream(out);
            StreamUtils.closeStream(in);
        }
    }
}

public class StreamUtils {
    // 关流
    public static void closeStream(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

//使用
SyncThread instace;
    ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    instace = SyncThread.getInstace(this, Constant.Sound_URL, fileName);
    singleThreadExecutor.execute(instace);
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页