gpt4 book ai didi

java - 如何在android中开始和停止多个文件的下载

转载 作者:行者123 更新时间:2023-12-01 14:51:17 26 4
gpt4 key购买 nike

我有一个带有按钮和标签的 Activity 。单击按钮后,我的应用程序必须下载多个文件(大约 9000 个)。如果用户再次单击按钮,下载必须停止,再次单击时,下载必须从头开始。

这就是我所做的:

Activity 中:

    file.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button)v;
if(canFile){
b.setText("Stop download");
changeLabelInfo("Getting file list...");
labelFile.setVisibility(View.VISIBLE);
fileTask.start();
}else{
b.setText("Download files");
if(fileTask.isAlive()){
fileTask.interrupt();
fileTask = null;
fileTask = new UpdateFilesThread(this);
}
labelFile.setVisibility(View.INVISIBLE);
Kernel.setManualUpdate("file",false);
}
canFile = !canFile;
}
});

必须下载文件的线程是UpdateFilesThread:

public class UpdateFilesThread extends Thread{
private MainActivity activity;
private final String rootPath = "/mnt/local/";
public UpdateFilesThread(MainActivity activity){
this.activity = activity;
}


public void run(){
String json = getFilesURL();
JSONObject a = (JSONObject)JSONValue.parse(json);
boolean isZip = false,canDownload = true;
String[] keys = new String[]{"key1","key2","key3","key4"};

for(String key:keys){
Object folder = (Object)a.get(key);
if(folder instanceof JSONObject){
JSONObject fold = (JSONObject)folder;
for(Object path_o:fold.keySet()){
path = path_o.toString().replace(" ", "%20");
if(local.endsWith(".php")){
isZip = true;
try {
Jsoup.connect(mywebserviceURL).data("path",path).timeout(0).post(); // If php generate zip containing php file
} catch (IOException e) {
canDownload = false;
}
}
if(canDownload){
try{
if(downloadFromUrl(path,isZip))
//SAVE URL DOWNLOADED
}catch(Exception e){
e.printStackTrace();
}
}
canDownload = true;
isZip = false;
}
}
}
a.remove(key);
}

private String getFilesURL(){
try {

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("type", new StringBody("all"));
HttpPost post = new HttpPost("mywebserviceURL");
post.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);

return EntityUtils.toString(response.getEntity());
} catch (UnsupportedEncodingException e) {
Support.writeError(e, null);
e.printStackTrace();
return "";
} catch (ClientProtocolException e) {
Support.writeError(e, null);
e.printStackTrace();
return "";
} catch (ParseException e) {
Support.writeError(e, null);
e.printStackTrace();
return "";
} catch (IOException e) {
Support.writeError(e, null);
e.printStackTrace();
return "";
}
}
public boolean downloadFromUrl(String path,boolean isZip){
InputStream is = null;
FileOutputStream fos = null;
String localFilename = rootPath+path;
String local = isZip?rootPath+"tmp.zip":localFilename;


boolean return_ = false;
try {
URL url = new URL(isZip?mywebserviceURLZip:mywebserviceURLZip+path);
URLConnection urlConn = url.openConnection();
urlConn.setReadTimeout(0);
is = urlConn.getInputStream();
fos = new FileOutputStream(local);

byte[] buffer = new byte[51200];
int len;

while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
is.close();
if(isZip){
ZipFile zip = new ZipFile(local);
zip.extractAll(rootPath);
new File(local).delete();
}

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

当用户单击两次按钮(停止下载并重新开始)时,我的问题就出现了。提示错误说线程已经启动并且正在运行..我该如何解决它?我知道 asyncTask 应该更好,但我有问题,因为我的应用程序中有很多线程正在运行,并且设备的性能很差。有可能完全停止一个线程吗?还有其他更好的解决方案吗?

最佳答案

尝试实现 AsyncTask 。当用户第一次点击按钮时,调用任务的 execute (Params... params) 。第二次点击时,调用任务的 cancel (boolean mayInterruptIfRunning) 。将下载功能放在任务的 doInBackground (Params... params)

关于java - 如何在android中开始和停止多个文件的下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14816214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com