在我的应用程序中,我从远程服务器下载数据。我的数据大小为289 MB。在下载数据时,它会多次发生,下载停止,并从头开始下载。现在我想实现这样一个下载逻辑,它将恢复前一点的数据下载。 HttpURLConnection连接; class myasyncTask扩展了AsyncTask {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
lenghtOfFile=0;
total=0;
publishProgress(""+0);
showDialog(prog_bar);
}
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(pDialog.isShowing()){
dismissDialog(prog_bar);
}
SavePreferences("MEM2", lenghtOfFile);
if(total==lenghtOfFile){
Intent i=new Intent(MainActivity.this,audio.class);
startActivity(i);
}
pDialog.setProgress(0);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
URL url = new URL("");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
lenghtOfFile= connection.getContentLength();
FileOutputStream file = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/com.test.AndroidApp/" ,"android"));
InputStream in = connection.getInputStream();
byte[] data = new byte[1024];
int count= 0;
while ((count = in.read(data)) > 0) {
total += count; //total = total + len1
publishProgress("" + (int)((total*100)/lenghtOfFile));
file.write(data, 0,count);
}
file.close();
} catch (Exception e) {
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
pDialog.setProgress(Integer.parseInt(values[0]));
}
}