每个都有多个下载和更新进度栏

时间:2018-06-25 06:00:32

标签: android android-progressbar android-file android-download-manager

我对书有回收利用的看法。当用户单击一本书时,将启动带有2个按钮的片段,用户可以在其中进行以下操作:

  1. 读这本书
  2. 下载图书

如果用户要下载图书,则进度条将显示在片段的顶部,并显示标题:

Downloading page <n> of <total pagecount>

标题和进度条将更新,直到下载完成。用户可以按下“后退”按钮并下载另一本书。如果用户选择返回第一本书,则该片段应以下载的最新进度启动。

如何最好地实现此功能?

我尝试过的一些事情:

  1. 我尝试在片段中创建一个AsyncTask。但是当用户按下“后退”时,任务停止。
  2. 我创建了一个界面,并让活动进行下载而不是执行片段。我现在可以进行多次下载,但是在从活动中检索进度并将其显示在片段中时遇到了问题。有时,进度条和标题根本不会更新。另外,我的应用程序变得非常缓慢,有时甚至没有响应。

在片段中:

fragmentCallback!!.downloadBook(activity, book, object : DownloadBookProgressCallback {
                            override fun downloadProgress(page: Int) {
                                uiThread {
                                    progressBar!!.progress = page
                                    progressText!!.text = "Downloading page " + page + " of " + book.content!!.pageCount!!
                                }
                            }
                        })

接口:

interface FragmentCallback {
    fun downloadBook(context: Context, book: ResourcesList, downloadBookProgressCallback: DownloadBookProgressCallback)
    fun getDownloadProgress(bookId: Int):Int
    fun getDownloadStatus (bookId: Int) : Boolean
    fun getMaxPages (bookId: Int) : Int
}

活动中

 override fun downloadBook(context: Context, book: ResourcesList, downloadBookProgressCallback: DownloadBookProgressCallback) {
            val downloadInstance = DownloadBook()
            activeDownloadBooks[book.id] = downloadInstance
            downloadInstance.gettingPages(context, book.content!!.pageCount!!, book, downloadBookProgressCallback)
    }

    override fun getDownloadStatus(bookId: Int): Boolean {
        val retrieveInstance = activeDownloadBooks[bookId]

        if (retrieveInstance != null) {
            return retrieveInstance.getDownloadStatus()
        }

        return false
    }

    override fun getDownloadProgress(bookId: Int): Int {
        val retrieveInstance = activeDownloadBooks[bookId]
        if (retrieveInstance!!.getDownloadStatus()) {
            return retrieveInstance.getProgress()
        } else {

        }
        return 0
    }

    override fun getMaxPages(bookId: Int): Int {
        val retrieveInstance = activeDownloadBooks[bookId]
        return retrieveInstance!!.getMaxPages()
    }

下载课程

class DownloadBook {
    private var isDownloading : Boolean = false
    private var currentPage = 0
    private var maxPages = 0
    var sp : SharedPreferences? = null

    fun gettingPages(ctx : Context, pageCount : Int, book : ResourcesList, callback: DownloadBookProgressCallback) {
        maxPages = pageCount
        for (page in 1..pageCount) {
            isDownloading = true
            println("getting page " + page + " " + book.name)
            currentPage = page
            callback.downloadProgress(page)
            var a = book.content!!.pageURI + page

            println(a)
            val bmap: Bitmap = Picasso.get().load(a).get()


            val cw = ContextWrapper(ctx)

            val directory = cw.getDir("BookId" + book.id.toString(), Context.MODE_PRIVATE)
            val mypath = File(directory, "page" + page + ".jpg")

            var fos: FileOutputStream
            try {
                fos = FileOutputStream(mypath)
                bmap.compress(Bitmap.CompressFormat.PNG, 100, fos)
                fos.close()
            } catch (e: Exception) {
                Log.e("DOWNLOAD_BOOK", e.message, e)
                e.printStackTrace()
            }

        }

        sp = PreferenceManager.getDefaultSharedPreferences(ctx)
        val editor = sp!!.edit()
        println("Saving to shared prefs: " + book.name)
        editor.putString(book.id.toString(), book.content!!.pageCount.toString())
        editor.apply()

        isDownloading = false
    }



    fun getDownloadStatus() : Boolean {
        return isDownloading
    }

    fun getProgress() : Int {
        return currentPage
    }

    fun getMaxPages() : Int {
        return maxPages
    }
}

在片段中:检查是否存在当前的下载实例

fragmentCallback = activity as FragmentCallback

        if (fragmentCallback != null) {

            doAsync {
                var stillDownloading = fragmentCallback!!.getDownloadStatus(book_id)
                var maxPages = 0

                println("Stilldownloading: " + stillDownloading + " Book Id :" + book_id )

                if (stillDownloading) {

                    uiThread {
                        progressBar!!.visibility = View.VISIBLE
                        progressText!!.visibility = View.VISIBLE
                        maxPages = fragmentCallback!!.getMaxPages(book_id)
                        progressBar!!.max = maxPages
                        ViewCompat.setBackgroundTintList(
                                dloadButton!!,
                                ColorStateList.valueOf(getColorWrapper(activity, R.color.color_grey)))
                        dloadButton!!.isEnabled = false
                    }

                        while (stillDownloading) {
                            var dloadPage = fragmentCallback!!.getDownloadProgress(book_id)
                            if (dloadPage > 0) {

                                uiThread {
                                    progressBar!!.progress = dloadPage
                                    progressText!!.text = "Downloading page " + dloadPage + " of " + maxPages
                                }

                                stillDownloading = fragmentCallback!!.getDownloadStatus(book_id)

                            }
                        }

                }
            }

        }

0 个答案:

没有答案