大家好 求一个点击按钮下载 apk的例子
新人不会实现 求一个例子谢谢了, 现在要一个这个功能
点击按钮下载一个apk 到手机中
------解决方案--------------------
送你个下载类吧
new出来这个对象,调用startdown方法就可以下载了,
public class DownLoadOneApp {
Context context;
private String packageName;
private String apkname;
private String downloadurl;
private String outfilepath;
//the progress is displayed.
public int progress=0;
//the app file
private File apkfile;
//the app file size
private long fileSize;
//means download size
private long downLoadFileSize;
public DownLoadOneApp(Context context,String packageName,String outfilepath,String apkname,String downloadurl) {
this.context = context;
this.packageName=packageName;
this.downloadurl=downloadurl;
this.outfilepath=outfilepath;
this.apkname=apkname;
}
/**
* @param outpath
* @param url
*/
public boolean startdownfile() {
final String downurl=downloadurl;
final String outpath=outfilepath+apkname+".tmp";
apkfile=new File(outpath);
File pathfile=new File(outfilepath);
if(!pathfile.exists()){
Log_D("the path "+outfilepath+" is not exists,so we mkdirs it");
if(pathfile.mkdirs()){
Log_D("mkdirs "+outfilepath+" success!");
}else{
Log_D("mkdirs "+outfilepath+" occur error!");
return false;
}
}
new Thread() {
@Override
public void run() {
try {
// 下载文件,参数:第一个URL,第二个存放路径
down_file(downurl,apkfile);
} catch (ClientProtocolException e) {
downloadOccurError("ClientProtocolException");
Log_E("ClientProtocolException");
} catch (IOException e) {
downloadOccurError("IOException");
Log_E("IOException");
}
}
}.start();
return true;
}
/**
* download file
* @param url
* @param path
* @throws IOException
*/
private void down_file(String url, File apkfile) throws IOException {
// 获取文件名
URL myURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Referer", myURL.toString());
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
// 设置范围,格式为Range:bytes x-y;
conn.connect();
this.fileSize = conn.getContentLength();// 根据响应获取文件大小
// conn.setRequestProperty("RANGE", "bytes=0-"+fileSize);
InputStream is = conn.getInputStream();
if (this.fileSize <= 0){
Log_E("无法获取文件大小");return;
}
if (is == null){
Log_E("输入流为空");
return;
}
FileOutputStream fos = new FileOutputStream(apkfile);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(fos);
// 把数据存入路径+文件名
byte buf[] = new byte[16348];
downLoadFileSize = 0;
sendMsg(0);
do {
// 循环读取
int numread = bis.read(buf);
// if(!JFQService.downflag)return;
if (numread == -1) {
break;
}
bos.write(buf, 0, numread);
downLoadFileSize += numread;
sendMsg(1);// 更新进度条
} while (true&&JFQService.downflag);
sendMsg(2);// 通知下载完成
is.close();
bis.close();
bos.close();
fos.close();
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {// 定义一个Handler,用于处理下载线程与UI间通讯
try {
if (!Thread.currentThread().isInterrupted()) {
// msg.what is flag.if flag is 0 means start download,flag