public class MainActivity extends AppCompatActivity {
// button to show progress Dialog
Button btnShowProgress;
// progress dialog
ProgressDialog proDaiog;
//File url to download
private static String url = "http://rvo/file/Ardu_Vid.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// show progress bar button
btnShowProgress = (Button) findViewById(R.id.button);
btnShowProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadFile().execute(url);
// running = true;
// runTimer();
}
});
} // end onCreate
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
proDaiog = new ProgressDialog(MainActivity.this);
//Set progress dialog title
proDaiog.setTitle("Downloading...");
//set progress dialog message
proDaiog.setMessage("");
proDaiog.setIndeterminate(false);
proDaiog.setMax(100);
proDaiog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
proDaiog.show();
}
@Override
protected String doInBackground(String... Url) {
try {
URL url = new URL(Url[0]);
URLConnection connection = url.openConnection();
connection.connect();
//Detect File Length
int fileLength = connection.getContentLength();
//Locate Storage Location
String filePath = Environment.getExternalStorageDirectory().getPath();
//Downloading File
InputStream input = new BufferedInputStream(url.openStream());
//Save the Download File
OutputStream output = new FileOutputStream(filePath + "/" + "Ardu_Vid.mp4");
byte data[] = new byte[fileLength];
long total = 0;
int count;
int s = 1;
// Long startTime = System.nanoTime();
// Log.e("Time-",""+startTime);
// Long elapsedTime = System.nanoTime() - startTime;
int sec = 0;
while ((count = input.read(data)) != -1) {
long Start_Timee = System.currentTimeMillis();
//long seconds = TimeUnit.MILLISECONDS.toSeconds(Start_Timee);
total += count;
output.write(data, 0, count);
//publish the progress
publishProgress((int) (total * 100 / fileLength));
/************ Get Duration of Downloading ***************/
Long End_Timee = System.currentTimeMillis();
Log.e("Time_End", "" + End_Timee);
long seccc = (End_Timee - Start_Timee);
Log.e("estimate ",""+seccc);
int seconds = (int) (seccc / 1000) % 60 ;
int minutes = (int) ((seccc / (1000*60)) % 60);
Log.e("elapse",""+minutes+":"+seconds);
}// end while
//close connection
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
proDaiog.setProgress(progress[0].intValue());
Log.i("proDaiog.setProgress", "" + progress[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
proDaiog.dismiss();
}
} // end Asyc
我想获得下载文件的剩余持续时间意味着如果你使用Idm,你在下载过程中看到剩余时间,我想在我的应用程序中显示相同的功能,但我坚持这个功能,我完成了整个应用程序,但我被困在只有剩余的持续时间,请帮助
发布于 2016-11-08 20:29:29
下面是一些让你上路的想法:
花费了多少时间
使用这些信息,计算剩余时间的估计应该非常简单。
换句话说:只需计算当前的下载速率X(您希望确保在执行此操作之前等待一段时间;以便使用某种程度上的“可靠”(又称稳定)数字)。然后计算下载剩余字节数Y所需的时间...给定X。
示例:假设您要下载一个大小为100MB的文件。
1分钟后,您检查并发现到目前为止已下载了10MB。所以,你的观察结果是:“下载速度大约是每分钟10MB”。
这意味着:如果速度没有太大变化,那么剩下的90MB还需要大约9分钟。
当然,这只是一个估计,基于观察到那个点的字节有多快。你应该确保你的代码不断更新它的预测(以一种“稳定”的方式,以避免每5秒给用户一个完全不同的预测)。
最后注意:请理解,我们不会为您实施您的ideas。它是你的应用,你的愿景,你的产品。
https://stackoverflow.com/questions/40486914
复制相似问题