如何获取sofar下载的字节?

时间:2016-06-11 07:05:59

标签: android

我使用这个library对于下载文件,这是非常棒的库,但我需要oncreate中的sofar下载字节(其进度参数)来更新onCreate中的进度条

  

这是我的代码:

public class MainActivity extends AppCompatActivity {

    private Button btnDownload;
    private ProgressBar prgDownload;
    private int downloadId;

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

        ////////////////////////////////////////////////////////////
        //================== Init Views  ===========
        ////////////////////////////////////////////////////////////

        btnDownload = (Button) findViewById(R.id.btnDownload);
        prgDownload = (ProgressBar) findViewById(R.id.prgDownload);
        final String savePath = FileDownloadUtils.getDefaultSaveRootPath() + File.separator + "angry.apk";
        final String url = "http://dl2.soft98.ir/mobile/Angry.Birds.Rio.2.6.1_Soft98.iR.apk";



        ////////////////////////////////////////////////////////////
        //================== Init Download Manager  ===========
        ////////////////////////////////////////////////////////////

        btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadId = createDownloadTask(url,savePath).start();
            }
        });

    }

    ////////////////////////////////////////////////////////////
    //================== Start Download Manager  ===========
    ////////////////////////////////////////////////////////////

    private BaseDownloadTask createDownloadTask(final String url , final String savePath){
        return FileDownloader.getImpl().create(url)
                .setPath(savePath)
                .setListener(new FileDownloadListener() {


                    @Override
                    protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                        Toast.makeText(MainActivity.this, "We are in pending ... ", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    protected void started(BaseDownloadTask task) {
                        super.started(task);
                    }

                    @Override
                =====>    protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                        prgDownload.setMax(totalBytes);
                        prgDownload.setProgress(soFarBytes);
                        btnDownload.setText("Stop");
                        btnDownload.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                FileDownloader.getImpl().pause(downloadId);
                            }
                        });

                    }

                    @Override
                    protected void completed(BaseDownloadTask task) {
                        Toast.makeText(MainActivity.this, "Download is Completed !!!", Toast.LENGTH_SHORT).show();
                        btnDownload.setText("Open");
                    }

                    @Override
                    protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                        btnDownload.setText("Resume");
                        btnDownload.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                createDownloadTask(url, savePath).start();
                                Toast.makeText(MainActivity.this, "Resume", Toast.LENGTH_SHORT).show();
                            }
                        });

                    }

                    @Override
                    protected void error(BaseDownloadTask task, Throwable e) {
                        Toast.makeText(MainActivity.this, "We have Error !!!", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    protected void warn(BaseDownloadTask task) {
                        Toast.makeText(MainActivity.this, "We have warn !!!", Toast.LENGTH_SHORT).show();

                    }
                });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FileDownloader.getImpl().pause(downloadId);
    }
}

0 个答案:

没有答案