Files
SNFLauncherMain_RockChip/mylibrary/src/main/java/com/android/download/DownloadAppTask.java
2025-11-06 10:55:48 +08:00

161 lines
5.7 KiB
Java

package com.android.download;
import android.content.Context;
import com.android.util.FileUtil;
import com.android.util.LogUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadAppTask extends Thread{
public static int threadCount = 3;
public static int runningThread = 3;
private String downurl;
private Context mContext;
/**存储路径*/
private String FILEPATH_STORAGE;
/**临时文件名称*/
public static String FILENAME_TMP="Nebula-app.bak";
/**正是文件名称*/
private static String FILENAME_APK="Nebula-app.apk";
private DownLoadAppListener mDownLoadAppListener;
private long startIndex = 0;
private long totalSize = 0;
public DownloadAppTask(Context context,String versionCode,String url,long fileSize,DownLoadAppListener downLoadAppListener){
mContext = context;
this.downurl = url;
this.totalSize = fileSize;
this.mDownLoadAppListener = downLoadAppListener;
this.FILEPATH_STORAGE = FileUtil.getBakPath(mContext,1) ;
FILENAME_APK=versionCode+"-"+FILENAME_APK;
}
@Override
public void run() {
super.run();
try {
File tempfile =new File(FILEPATH_STORAGE,FILENAME_TMP);
if(tempfile.exists()){
startIndex = tempfile.length();
}else {
tempfile.createNewFile();
}
HttpURLConnection urlConnection;
URL url = new URL(downurl);
// URL url = new URL(" https://ik-cos-1258208609.cos.ap-guangzhou.myqcloud.com/Chrome.apk");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(2000*10); //设置连接超时事件为5秒
urlConnection.setRequestMethod("GET"); //设置请求方式为GET
//设置用户端可以接收的媒体类型
urlConnection.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, " +
"image/pjpeg, application/x-shockwave-flash, application/xaml+xml, " +
"application/vnd.ms-xpsdocument, application/x-ms-xbap," +
" application/x-ms-application, application/vnd.ms-excel," +
" application/vnd.ms-powerpoint, application/msword, */*");
urlConnection.setRequestProperty("Accept-Language", "zh-CN"); //设置用户语言
urlConnection.setRequestProperty("Charset", "UTF-8"); //设置客户端编码
//设置用户代理
urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; " +
"Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727;" +
" .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
//设置下载位置
urlConnection.setRequestProperty("Range", "bytes=" + startIndex + "-" + (totalSize-1));
LogUtils.loge("totalSize:"+totalSize+"");
LogUtils.loge("getResponseCode:"+urlConnection.getResponseCode()+"");
InputStream in = urlConnection.getInputStream();
RandomAccessFile raf = new RandomAccessFile(tempfile.getPath(), "rwd");
raf.seek(startIndex);
int len = 0;
int progress=0;
byte buf[] =new byte[1024];
while((len = in.read(buf))!=-1){
raf.write(buf, 0, len);
progress+=len;
LogUtils.loge("downlaod progress:"+progress);
}
in.close();
raf.close();
File apkFile = new File(FILEPATH_STORAGE,FILENAME_APK);
LogUtils.loge("downlaod:"+tempfile.length());
if(tempfile.length()==totalSize){
tempfile.renameTo(apkFile);
tempfile.delete();
//下载完成
if(mDownLoadAppListener!=null){
mDownLoadAppListener.onDowanlaodResult(0,apkFile.getPath());
}
}else {
if(mDownLoadAppListener!=null){
mDownLoadAppListener.onDowanlaodResult(-1,null);
}
}
} catch (Exception e) {
e.printStackTrace();
if(mDownLoadAppListener!=null){
mDownLoadAppListener.onDowanlaodResult(-1,null);
}
}
}
// public boolean executeAppInstall(String apkPath){
// boolean result = false;
// try {
// Runtime.getRuntime().exec("su");
// String command = "pm install -r " + apkPath + "\n";
// Process process = Runtime.getRuntime().exec(command);
// BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// String msg = "";
// String line;
// while ((line = errorStream.readLine()) != null) {
// msg += line;
// }
// if (!msg.contains("Failure")) {
// result = true;
// }
//
// } catch (IOException e) {
// e.printStackTrace();
// }
// return result;
// }
public interface DownLoadAppListener{
public void onDowanlaodResult(int status,String filePath);
}
}