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

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

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

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

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

/**
* 远程下载安装Android程序
*
* @ClassName InstallOnlineActivity
* @author Jet
* @date 2012-9-14
*/
public class InstallOnlineActivity extends Activity {
private TextView mTextView;
private EditText mEditText;
private Button mButton;
private String currentFilePath = "";
private String currentTempFilePath = "";
private String strURL = "";
private String fileEx = "";
private String fileName = ""; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.installonline);
mTextView = (TextView) findViewById(R.id.installonline_text1);
mEditText = (EditText) findViewById(R.id.installonline_edittext1);
mButton = (Button) findViewById(R.id.installonline_button1);
mButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 将文件下载到本地
mTextView.setText("下载中...");
strURL = mEditText.getText().toString();
// 截取文件后缀
fileEx = strURL.substring(strURL.lastIndexOf('.') + 1,
strURL.length()).toLowerCase();
// 截取文件名
fileName = strURL.substring(strURL.lastIndexOf('/') + 1,
strURL.lastIndexOf('.'));
getFile(strURL);
} });
} private void getFile(final String strPath) {
if (currentFilePath.equals(strPath)) {
getDataSource(strPath);
}
currentFilePath = strPath;
Runnable r = new Runnable() { @Override
public void run() {
getDataSource(strPath);
}
};
new Thread(r).start();
} private void getDataSource(String url) {
if (!URLUtil.isNetworkUrl(url)) {
mTextView.setText("请填写正确的URL");
} else {
try {
URL myUrl = new URL(url);
// 取得连接
URLConnection conn = myUrl.openConnection();
// 连接
conn.connect();
// 获得输入流
InputStream is = conn.getInputStream();
if (is == null) {
throw new RuntimeException("stream is null");
}
// 创建临时文件
File myTempFile = File.createTempFile(fileName, "." + fileEx);
// 取得临时文件存放路径
currentTempFilePath = myTempFile.getAbsolutePath();
FileOutputStream fos = new FileOutputStream(myTempFile);
byte[] buf = new byte[128];
do {
// 返回现在所读缓冲区的大小
int numread = is.read(buf);
if (numread <= 0) {
break;
}
fos.write(buf, 0, numread);
} while (true);
// 打开文件进行安装
openFile(myTempFile);
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} private void openFile(File file) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
String type = getMimeType(file);
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent);
} private String getMimeType(File file) {
String type = "";
String fname = file.getName();
// 获得扩展名
String end = fname
.substring(fname.lastIndexOf('.') + 1, fname.length())
.toLowerCase();
// 按扩展名的类型决定MimeType
if ("m4a".equals(end) || "mp3".equals(end) || "mid".equals(end)
|| "xmf".equals(end) || "ogg".equals(end) || "wav".equals(end)) {
type = "audio";
} else if ("3gp".equals(end) || "mp4".equals(end)) {
type = "video";
} else if ("jpg".equals(end) || "gif".equals(end) || "png".equals(end)
|| "jpeg".equals(end) || "bmp".equals(end)) {
type = "image";
} else if ("apk".equals(end)) {
type = "application/vnd.android.package-archive";
} else {
type = "*";
}
if ("apk".equals(end)) { } else {
type += "/*";
}
return type;
}
private void delFile(String fileName){
File file = new File(fileName);
if(file.exists()){
file.delete();
}
}
@Override
protected void onPause() {
mTextView = (TextView) findViewById(R.id.installonline_text1);
mTextView.setText("下载成功");
super.onPause();
}
@Override
protected void onResume() {
//删除临时文件
delFile(currentTempFilePath);
super.onResume();
}
}

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. vijos1603迷宫

    这题的构思太巧妙了: 经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值    把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j. ...

  2. What is Cross Linux From Scratch?

    /**************************************************************************** * What is Cross Linux ...

  3. 多线程程序设计学习(10)Future pattern

    Future pattern[订单取货模式] 一:Future pattern的参与者--->Client(客户需求)--->Host(蛋糕门店)--->Data(票据和蛋糕的接口) ...

  4. 【转】 C++中如何在一个构造函数中调用另一个构造函数

    在C++中,一个类的构造函数没法直接调用另一个构造函数,比如: #ifndef _A_H_ #define _A_H_ #include <stdio.h> #include <ne ...

  5. MySQL 视图知识点小结

    视图本身是一个虚拟表,不存放任何数据.在使用SQL语句访问视图的时候,它返回的数据是MySQL从其他表中生成的.视图和表在同一个命名空间, MySQL在很多地方对于视图和表是同样对待的.不过视图和表也 ...

  6. Velocity是如何工作的

    当在一个应用程序或者servlet中使用Velocity的时候,你要做如下几个工作: 初始化Velocity.Velocity提供2种使用形式,不管是单一实例模式还是单独运行实例模式,初始化过程都只做 ...

  7. 文件I/O操作(2)

    lseek函数原型为int lseek(int fd,int offset, int whence),fd为定位文件的描述符,offset为偏移量,如果是正数,则向文件末尾偏移,负数时,则向文件头偏移 ...

  8. 京东拍拍网 笔试 搞java的去考C++ 苦逼

    1.用C实现数字逆转,用递归实现,很简单. package 京东; public class Main { private static int a=0; public static void fun ...

  9. C++空类以及没有成员变量的类的大小

    关于C++中空类的大小为1,我们大家都有所了解,但是除了空类之外的其他一些没有成员变量的类的大小,还是有很多不明之处的. 我们来看如下一个例子: #include<iostream> us ...

  10. NOIP2011 Mayan游戏

    3 Mayan游戏 题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上 ...