package com.weikou.wpc.superad.util;
|
|
import java.io.File;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.net.Uri;
|
import android.os.AsyncTask;
|
import android.util.Log;
|
import android.widget.TextView;
|
|
import com.weikou.wpc.superad.service.DownLoadFileService;
|
import com.ysh.wpc.appupdate.download.DownLoadFile;
|
import com.ysh.wpc.appupdate.download.FileUtils;
|
import com.ysh.wpc.appupdate.util.StringUtils;
|
|
/**
|
* @author weikou2015 涓嬭浇宸ュ叿绫�
|
*/
|
public class DownFiles extends AsyncTask<String, Integer, String> implements
|
DownLoadFile.FileProgressListener {
|
// TextView tv;
|
Context context;
|
IProgress progress;
|
|
public DownFiles(Context context, IProgress progress) {
|
// this.tv = tv;
|
this.context = context;
|
this.progress = progress;
|
}
|
|
@Override
|
protected void onProgressUpdate(Integer... values) {
|
super.onProgressUpdate(values);
|
if (progress != null && values[0] % 1000000 == 0)
|
progress.getProgress(values[0] / 1000000);
|
// if (values[0] / 1000000 == 100 && values[0] == 0) {
|
// tv.setText("完成");
|
// } else {
|
// tv.setText("下载" + values[0] / 1000000 + "%");
|
// }
|
|
}
|
|
@Override
|
protected String doInBackground(String... params) {
|
String url = params[0];
|
// 下载
|
DownLoadFile dl = new DownLoadFile();
|
String[] urls = url.split("/");
|
String name = "";
|
if (urls.length > 0)
|
name = FileUtils.getRootPath(context) + File.separator
|
+ urls[urls.length - 1];
|
else
|
name = FileUtils.getRootPath(context) + File.separator
|
+ System.currentTimeMillis() + ".apk";
|
System.out.println("APK名称:" + name);
|
try {
|
File f = dl.downLoadFile(this, name, url, context);
|
return f.getPath();
|
} catch (Exception e) {
|
DownLoadFileService.j = -1;
|
}
|
return "";
|
}
|
|
// 完成网络请求
|
@Override
|
protected void onPostExecute(String result) {
|
super.onPostExecute(result);
|
if (!StringUtils.isNullOrEmpty(result) && result.endsWith(".apk")) {
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
intent.setDataAndType(Uri.fromFile(new File(result)),
|
"application/vnd.android.package-archive");
|
context.startActivity(intent);
|
}
|
}
|
|
@Override
|
public void update(int progress) {
|
publishProgress(progress);
|
}
|
|
public interface IProgress {
|
public void getProgress(int p);
|
|
}
|
}
|