Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在ProgressWheel中设置下载文件的进度_Android_Android Asynctask_Download - Fatal编程技术网

Android 在ProgressWheel中设置下载文件的进度

Android 在ProgressWheel中设置下载文件的进度,android,android-asynctask,download,Android,Android Asynctask,Download,我有一个列表视图。当用户选择我的列表视图中的一项时,我打开DetailActivity,用户可以在该活动中下载文件,我使用ProgressWheel显示用户下载百分比 我的问题是,当用户关闭活动然后返回活动时,ProgressWheel不再更新。 以下是我的代码:(我使用AsynchTask下载文件) /** *要下载文件的后台异步任务 * */ 类DownloadTask扩展了AsyncTask{ /** *在后台线程中下载文件 * */ @凌驾 受保护的布尔doInBackground(字符

我有一个列表视图。当用户选择我的列表视图中的一项时,我打开DetailActivity,用户可以在该活动中下载文件,我使用
ProgressWheel
显示用户下载百分比

我的问题是,当用户关闭活动然后返回活动时,
ProgressWheel
不再更新。 以下是我的代码:(我使用AsynchTask下载文件)

/**
*要下载文件的后台异步任务
* */
类DownloadTask扩展了AsyncTask{
/**
*在后台线程中下载文件
* */
@凌驾
受保护的布尔doInBackground(字符串…参数){
试一试{
URL=null;
试一试{
字符串链接=参数[0];
fileExtention=getFileExtention(link);
url=新url(链接);
}捕获(例外e){
e、 printStackTrace();
返回false;
}
HttpURLConnection连接=(HttpURLConnection)url
.openConnection();
//为httpUrlConnection设置一些参数
setConnectTimeout(JSONConstants.connection\u TIMEOUT);
setReadTimeout(JSONConstants.READ\u TIMEOUT);
connection.connect();//连接在此完成。!
//从服务器获取并捕获输入流对象。
InputStream InputStream=connection.getInputStream();
int lenghtOfFile=connection.getContentLength();
String PATH=Environment.getExternalStorageDirectory()
+“/下载/”;
文件=新文件(路径);
如果(!file.exists()){
mkdirs()文件;
}
File outputFile=新文件(文件名为“fileTitle”)
+“.mp3”);
FileOutputStream FileOutputStream=新的FileOutputStream(
输出文件);
字节[]缓冲区=新字节[4096];
整数长度=0;
长总计=0;
而((长度=inputStream.read(缓冲区))!=-1){
总长度+=长度;
//发布进度。。。。
//在此之后,将调用onProgressUpdate
如果(长度工具>0)
出版进度(“”
+(整数)(总计*100)/长度单位);
//写入FileOutputStream。
写入(缓冲区,0,长度);
}
fileOutputStream.flush();
fileOutputStream.close();
inputStream.close();
}捕获(例外e){
e、 printStackTrace();
返回false;
}
返回true;
}
私有void publishProgress(字符串百分比){
//将进度指示器设置为最大值
//当前完成百分比和“确定”
//陈述
downloadProgressWheel.setProgress(整数.valueOf(百分比));
}

我如何设置我的
ProgressWheel
在用户返回其关联活动时仍会更新。

AsyncTask
在返回活动时是否正在运行?并且您希望在下载完成后隐藏该进度轮?在这种情况下,请将其隐藏在Asyn的后执行方法@ρƏσѕρєK中,我确信
AsyncTask
正在运行,因为过了一段时间,我可以看到下载已完成,您的progresswheel是否在某个百分比上卡住了?好的,如果在异步任务的on-post-execute方法中将其设置为100会怎么样?
/**
 * Background Async Task to download file
 * */
class DownloadTask extends AsyncTask<String, String, Boolean> {
    /**
     * Downloading file in background thread
     * */
    @Override
    protected Boolean doInBackground(String... params) {
        try {
            URL url = null;
            try {
                String link = params[0];
                fileExtention = getFileExtention(link);
                url = new URL(link);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            // set some parameters for httpUrlConnection
            connection.setConnectTimeout(JSONConstants.CONNECTION_TIMEOUT);
            connection.setReadTimeout(JSONConstants.READ_TIMEOUT);
            connection.connect(); // Connection Complete here.!
            // Get from Server and Catch In Input Stream Object.
            InputStream inputStream = connection.getInputStream();
            int lenghtOfFile = connection.getContentLength();

            String PATH = Environment.getExternalStorageDirectory()
                    + "/download/";
            File file = new File(PATH); 
            if (!file.exists()) {
                file.mkdirs();
            }
            File outputFile = new File(file, "fileTitle"
                    + ".mp3" );
            FileOutputStream fileOutputStream = new FileOutputStream(
                    outputFile);

            byte[] buffer = new byte[4096];
            int length = 0;
            long total = 0;
            while ((length = inputStream.read(buffer)) != -1) {
                total += length;
                // publishing the progress....
                // After this onProgressUpdate will be called
                if (lenghtOfFile > 0)
                    publishProgress(""
                            + (int) ((total * 100) / lenghtOfFile));

                // Write In FileOutputStream.
                fileOutputStream.write(buffer, 0, length);
            }
            fileOutputStream.flush();
            fileOutputStream.close();
            inputStream.close();

        }  catch (Exception e) {    
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private void publishProgress(String percent) {
        // Sets the progress indicator to a max value, the
        // current completion percentage, and "determinate"
        // state
        downloadProgressWheel.setProgress(Integer.valueOf(percent));

    }