//下载apk程序代码
protected File downLoadFile(String httpUrl) {
// TODO Auto-generated method stub
final String fileName = "updata.apk";
File tmpFile = new File("/sdcard/update");
if (!tmpFile.exists()) {
tmpFile.mkdir();
}
final File file = new File("/sdcard/update/" + fileName); try {
URL url = new URL(httpUrl);
try {
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[256];
conn.connect();
double count = 0;
if (conn.getResponseCode() >= 400) {
Toast.makeText(Main.this, "连接超时", Toast.LENGTH_SHORT)
.show();
} else {
while (count <= 100) {
if (is != null) {
int numRead = is.read(buf);
if (numRead <= 0) {
break;
} else {
fos.write(buf, 0, numRead);
} } else {
break;
} }
} conn.disconnect();
fos.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block e.printStackTrace();
} return file;
}
//打开APK程序代码 private void openFile(File file) {
// TODO Auto-generated method stub
Log.e("OpenFile", file.getName());
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
startActivity(intent);
}

最新文章

  1. .NET操作Xml类
  2. Yii源码阅读笔记(二十一)——请求处理流程
  3. CentOS 7系统挂载NTFS分区的移动硬盘(转载及体验 CentOS6.5系统挂载NTFS分区的移动硬盘)
  4. MVC ckeditor的基本使用
  5. POJ 1149 PIGS ★(经典网络流构图)
  6. HTTP请求、响应报文格式
  7. 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)
  8. ZOJ- 3640 Help Me Escape
  9. Zookeeper 1、Zookeeper 定义与工作原理
  10. Java学习笔记50:JSONObject与JSONArray的使用
  11. PHP根据设备类型自动跳转相应网址页面,这个现在实用
  12. 【一天一道LeetCode】#232. Implement Queue using Stacks
  13. linux 下创建共享文件夹
  14. Windows10远程桌面Ubuntu16.04
  15. Install weblogic in silent mode
  16. Navicat for Oracle 绿色版 连接 Oracle 12c
  17. IdentityServer4-前后端分离的授权验证(六)
  18. [整理] mysql操作
  19. linq to sql 项目移植后,数据库实体类需要重新创建?
  20. aspectj

热门文章

  1. Set集合对象比较两个元素的方法
  2. 使用CSS3的appearance属性改变元素的外观
  3. Codeforces Round #207 (Div. 1) A. Knight Tournament(STL)
  4. BZOJ3442: 学习小组
  5. linux系统中errno与error对照表
  6. Working with HTTP
  7. The Unsolvable Problem
  8. WinForm timer控件
  9. Git基本命令行操作
  10. IE10以下的IE浏览器在form表单提交、a标签等场景下,接收application/json类型的响应时,会提示是否要下载该json文件