原文:http://blog.csdn.net/jasper_success/article/details/7984065

第一步:使用java.net的URLConnection对象来创建连接

第二步:通过InputStream将下载的文件写入存储卡内缓存

第三步:下载完毕之后,通过自定义的openFile()方法打开文件,判断文件类型,若为APK,开始安装

第四步:准备离开Installer程序的同时,通过自制的delFile()方法,删除缓存内文件

  1. /**
  2. * 远程下载安装Android程序
  3. *
  4. * @ClassName InstallOnlineActivity
  5. * @author Jet
  6. * @date 2012-9-14
  7. */
  8. public class InstallOnlineActivity extends Activity {
  9. private TextView mTextView;
  10. private EditText mEditText;
  11. private Button mButton;
  12. private String currentFilePath = "";
  13. private String currentTempFilePath = "";
  14. private String strURL = "";
  15. private String fileEx = "";
  16. private String fileName = "";
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.installonline);
  22. mTextView = (TextView) findViewById(R.id.installonline_text1);
  23. mEditText = (EditText) findViewById(R.id.installonline_edittext1);
  24. mButton = (Button) findViewById(R.id.installonline_button1);
  25. mButton.setOnClickListener(new OnClickListener() {
  26.  
  27. @Override
  28. public void onClick(View v) {
  29. // 将文件下载到本地
  30. mTextView.setText("下载中...");
  31. strURL = mEditText.getText().toString();
  32. // 截取文件后缀
  33. fileEx = strURL.substring(strURL.lastIndexOf('.') + 1,
  34. strURL.length()).toLowerCase();
  35. // 截取文件名
  36. fileName = strURL.substring(strURL.lastIndexOf('/') + 1,
  37. strURL.lastIndexOf('.'));
  38. getFile(strURL);
  39. }
  40.  
  41. });
  42. }
  43.  
  44. private void getFile(final String strPath) {
  45. if (currentFilePath.equals(strPath)) {
  46. getDataSource(strPath);
  47. }
  48. currentFilePath = strPath;
  49. Runnable r = new Runnable() {
  50.  
  51. @Override
  52. public void run() {
  53. getDataSource(strPath);
  54. }
  55. };
  56. new Thread(r).start();
  57. }
  58.  
  59. private void getDataSource(String url) {
  60. if (!URLUtil.isNetworkUrl(url)) {
  61. mTextView.setText("请填写正确的URL");
  62. } else {
  63. try {
  64. URL myUrl = new URL(url);
  65. // 取得连接
  66. URLConnection conn = myUrl.openConnection();
  67. // 连接
  68. conn.connect();
  69. // 获得输入流
  70. InputStream is = conn.getInputStream();
  71. if (is == null) {
  72. throw new RuntimeException("stream is null");
  73. }
  74. // 创建临时文件
  75. File myTempFile = File.createTempFile(fileName, "." + fileEx);
  76. // 取得临时文件存放路径
  77. currentTempFilePath = myTempFile.getAbsolutePath();
  78. FileOutputStream fos = new FileOutputStream(myTempFile);
  79. byte[] buf = new byte[128];
  80. do {
  81. // 返回现在所读缓冲区的大小
  82. int numread = is.read(buf);
  83. if (numread <= 0) {
  84. break;
  85. }
  86. fos.write(buf, 0, numread);
  87. } while (true);
  88. // 打开文件进行安装
  89. openFile(myTempFile);
  90. is.close();
  91. } catch (MalformedURLException e) {
  92. e.printStackTrace();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98.  
  99. private void openFile(File file) {
  100. Intent intent = new Intent();
  101. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  102. intent.setAction(android.content.Intent.ACTION_VIEW);
  103. String type = getMimeType(file);
  104. intent.setDataAndType(Uri.fromFile(file), type);
  105. startActivity(intent);
  106. }
  107.  
  108. private String getMimeType(File file) {
  109. String type = "";
  110. String fname = file.getName();
  111. // 获得扩展名
  112. String end = fname
  113. .substring(fname.lastIndexOf('.') + 1, fname.length())
  114. .toLowerCase();
  115. // 按扩展名的类型决定MimeType
  116. if ("m4a".equals(end) || "mp3".equals(end) || "mid".equals(end)
  117. || "xmf".equals(end) || "ogg".equals(end) || "wav".equals(end)) {
  118. type = "audio";
  119. } else if ("3gp".equals(end) || "mp4".equals(end)) {
  120. type = "video";
  121. } else if ("jpg".equals(end) || "gif".equals(end) || "png".equals(end)
  122. || "jpeg".equals(end) || "bmp".equals(end)) {
  123. type = "image";
  124. } else if ("apk".equals(end)) {
  125. type = "application/vnd.android.package-archive";
  126. } else {
  127. type = "*";
  128. }
  129. if ("apk".equals(end)) {
  130.  
  131. } else {
  132. type += "/*";
  133. }
  134. return type;
  135. }
  136. private void delFile(String fileName){
  137. File file = new File(fileName);
  138. if(file.exists()){
  139. file.delete();
  140. }
  141. }
  142. @Override
  143. protected void onPause() {
  144. mTextView = (TextView) findViewById(R.id.installonline_text1);
  145. mTextView.setText("下载成功");
  146. super.onPause();
  147. }
  148. @Override
  149. protected void onResume() {
  150. //删除临时文件
  151. delFile(currentTempFilePath);
  152. super.onResume();
  153. }
  154. }

Android在线更新 远程安装程序的更多相关文章

  1. 在 Linux 中自动生成 Cordova/Phonegap for Android 的 APK 安装程序

    在 Linux 中自动生成 Cordova/Phonegap for Android 的 APK 安装程序 本贴首发于: http://xuekaiyuan.com/forum.php?mod=vie ...

  2. html5页面打包成App - Android或Iphone安装程序

    下载安装前端开发工具:HBuilder 官网下载:http://www.dcloud.io/ 根据官网说明安装 * 打开登录HBuilder,把做好的H5页面通过添加app项目把H5的文件夹加入进来( ...

  3. Android监听应用程序安装和卸载

    Android监听应用程序安装和卸载 第一. 新建监听类:BootReceiver继承BroadcastReceiver package com.rongfzh.yc; import android. ...

  4. PowerShell远程安装应用程序

    安装MSI包 使用PowerShell调用WMI对象,你可以执行下面的脚本来安装你的MSI安装包: $box="deviis01" #this is the name of you ...

  5. 您的手机上未安装应用程序 android 点击快捷方式提示未安装程序的解决

    最近APP出现一个很奇怪的问题,在Android 4.4.2和android 4.4.3系统上点击应用的快捷方式,打不开应用,而且会提示未安装程序. 确认了应用的MainActivity中设置了and ...

  6. Android 使用 adb命令 远程安装apk

    Android 使用 adb命令 远程安装apk ./adb devices 列出所有设备 ./adb connect 192.168.1.89 连接到该设备 ./adb logcat 启动logca ...

  7. 解决"Windows 安装程序不允许从远程桌面连接安装"

    msiexec /i c:\路径\安装程序 例如 msiexec /i c:\TortoiseSVN-1.7.2.22327-x64-svn-1.7.2.msi

  8. sencha touch 扩展篇之将sencha touch打包成安装程序(下)- 使用phonegap打包安装程序

        这讲我们来讲解下如何使用phonegapa创建项目环境并通过她们将sencha touch打包成app,这里我们只讲解打包android的apk,打包ios的过程有点类似,但是需要在mac环境 ...

  9. Android 开发环境安装配置手册

    本文指导,如何一步步搭建Android开发平台. 1  下载软件 n JDK 1.5+   到  http://java.sun.com/javase/downloads/index.jsp 下载 n ...

随机推荐

  1. javacript序列化表单数据

    在前端开发时,用到表单交互的比较多,在我们实现一些异步操作数据时,表单数据的序列化就显得尤为重要了.下面我们一起来看看如何进行序列化. 如,我们在进行提交表单时,地址栏里会显示这样的东东:name=z ...

  2. [POJ 2588] Snakes

    同swustoj 8 Snakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1015   Accepted: 341 ...

  3. ShareSDK 实现新浪微博分享(微信,QQ,新浪微博类似)

    1 . 分享(前提是集成了sdk,配置好了Key),只要实现点击事件,调用shareSina(); ShareSDK.initSDK(this); private void shareSina() { ...

  4. [.NET WebAPI系列02] WebAPI 中的HTTP通信

    [前言] 本节用于承上启下,通过第一节了解的WebAPI的基本语法,Controller CRUD方法的基本格式: 但很多场合,第一节中的Web API Controller方法返回的信息 过于简单, ...

  5. JVM的参数设置与OutOfMemoryError异常关系

    Java堆中存放Object对象数据,例如new出来的Object.当没有任何引用指向某对象时,该对象可能被垃圾回收.有关垃圾回收算法,可参考其他有关文章,网上很多.关于对象引用,按强弱还有强引用,软 ...

  6. mysqldump使用

    mysqldump常用于MySQL数据库逻辑备份. 1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dump ...

  7. 【转】提示框第三方库之MBProgressHUD iOS toast效果 动态提示框效果

    原文网址:http://www.zhimengzhe.com/IOSkaifa/37910.html MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单.方便,并且可以对显 ...

  8. (转载)【C++】new A和new A()的区别详解

    (转载)http://blog.csdn.net/xiajun07061225/article/details/8796257 我们在C++程序中经常看到两种new的使用方式:new A以及new A ...

  9. Android ViewTreeObserver简介

    Android ViewTreeObserver简介   一.结构 public final class ViewTreeObserver extends Object java.lang.Objec ...

  10. 【C/C++运行时库】 /MT /MTd /MD /MDd对C/C++运行库的影响

    欢迎转载 转载请注明出处:http://www.cnblogs.com/cuish/p/3146937.html 测试VS中[项目属性]-[C/C++]-[代码生成]选项中的[运行库]- [ /MT, ...