我正在嘗試構建一個應用程序,我正在同時播放視頻和圖像。每一個小時左右,應用程序必須從互聯網上下載資料。所以要使用新材料我試圖讓它重新啓動。我已經實現了一個runnable,並將代碼下載並重新啓動到run方法中。問題是應用程序在啓動後立即凍結 -在android中下載
synchronized public void run() {
download("http://www.justieltsshaddi.com/pankaj/list.txt",
Environment.getExternalStorageDirectory() + "/alpha/list.txt");
File beta = new File(Environment.getExternalStorageDirectory()
+ "/beta/");
File betalist = new File(beta + "/list.txt");
File alpha = new File(Environment.getExternalStorageDirectory()
+ "/alpha/");
File alphalist = new File(alpha + "/list.txt");
if (alphalist.lastModified() == betalist.lastModified()) {
return;
}
try {
FileReader inAlpha = new FileReader(alphalist);
BufferedReader br = new BufferedReader(inAlpha);
String s;
Toast.makeText(this, "Starting Download...", Toast.LENGTH_SHORT)
.show();
while ((s = br.readLine()) != null) {
download("http://www.justieltsshaddi.com/pankaj" + "/" + s,
Environment.getExternalStorageDirectory() + "/alpha/"
+ s);
}
// stop the activity to rename folders
Toast.makeText(this, "Download done. Restarting...",
Toast.LENGTH_SHORT).show();
Log.d("Pankaj", "Download Done");
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
Log.d("Pankaj", "MainActivity Killed");
// rename alpha to beta
deleteSubFolders(beta.toString());
alpha.renameTo(beta);
if (!alpha.exists()) {
alpha.mkdir();
}
File upper = new File(alpha + "/upper/");
if (!upper.exists())
upper.mkdirs();
File lower = new File(alpha + "/lower/");
if (!lower.exists())
lower.mkdirs();
// restart the activity
overridePendingTransition(0, 0);
startActivity(intent);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
任何幫助表示讚賞。提前致謝。
有一個小問題。在進一步處理之前,我需要等待下載完成。 –
你可以調用executeOnExecutor來序列化它,並有你的邏輯 – blganesh101
這就是我所做的。執行不會超出第一行。 –