其实是昨天反编译一个apk,给它添加一个自动更新的功能用到的。为了在smali下方便查看,代码写的不规范,反正到了smali都一个吊样~~~~

权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.INTERNET"/>
    //Start===========
public void updata(){
new Thread(){
@Override
public void run() {
request(apiUrl);
}
}.start(); }
String newapkName = "updata.apk";
String apiUrl = "http://192.168.1.101:8080/udate.txt";
//请求接口
public void request(String Httpurl){
URL url = null;
try {
url = new URL(Httpurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
String s = toString(is);
JSONObject jsonObject = new JSONObject(s);
Double versionCode = jsonObject.getDouble("versionCode"); //远程版本
String versionName = getVersionName();
double v = Double.parseDouble(versionName);
if(versionCode > v){
//更新下载
downLoadFile(jsonObject.getString("updateurl")); }
} catch (Exception e) {
e.printStackTrace();
} }
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1:
ShowUpdataDilg();
break; }
}
};
//版本提示对话框
public void ShowUpdataDilg() {
new AlertDialog.Builder(MainActivity.this)
.setTitle("提示")
.setMessage("是否安装最新版本? ")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Uodate();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { }
})
.show();
} //获取版本
private String getVersionName() throws Exception
{
PackageManager packageManager = getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(),0);
String version = packInfo.versionName;
return version;
} //安装apk
public void Uodate(){
// String str = "/" + newapkName;
// String fileName = Environment.getExternalStorageDirectory() + str;
// Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
// startActivity(intent); String str = "/sdcard/update/" + newapkName;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(str)),
"application/vnd.android.package-archive");
startActivity(intent); } //对话框确定
public void isUpdate(){
String str = "/" + newapkName;
String fileNames = Environment.getExternalStorageDirectory() + str;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileNames)), "application/vnd.android.package-archive");
startActivity(intent); } protected File downLoadFile(String httpUrl) {
File tmpFile = new File("/sdcard/update");
if (!tmpFile.exists()) {
tmpFile.mkdir();
}
final File file = new File("/sdcard/update/" + newapkName); 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(MainActivity.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;
}
}
}
fos.close();
is.close();
conn.disconnect();
//下载成功
handler.sendEmptyMessage(1);
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return file;
}
//输入流转换为String
public String toString(InputStream is) {
String result = null;
try {
//构造一个输出流(字节队列输出流)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//一个byte数组,用来存储得到的字节数据
byte[] bys = new byte[1024];
int len = 0;
//循环读出完整字节数据
while((len = is.read(bys)) != -1) {
baos.write(bys,0,len);
}
//byte数据转换为字符串
result = new String(baos.toByteArray());
is.close();
baos.close(); } catch (IOException e) {
e.printStackTrace();
}
return result;
}
//End===========

Android下载更新代码的更多相关文章

  1. Android下载更新的安装包以及九宫格界面

    继上篇博客,我接下来做的是一个九宫格界面,但是对之前的Splash页面我还有要说的就是,当出现网络异常.json解析异常或者没有更新的时候,我们都必须要跳转到我们的主页面,因为Splash页面仅是展示 ...

  2. github下载更新代码到本地

    git remote -v git fetch origin master git log -p  master.. origin/master 如果本地已修改需要 git stash git mer ...

  3. Android服务器——使用TomCat实现软件的版本检测,升级,以及下载更新进度!

    Android服务器--使用TomCat实现软件的版本检测,升级,以及下载更新进度! 算下来,TomCat服务器已经写了很长一段时间了,一直说拿他来搞点事 情,也一直没做,今天刚好有空,交流群还有人请 ...

  4. Android SDK Manager 无法下载更新,或者更新速度超慢,或者待安装包列表不显示

    解决方法: 转自 http://www.cnblogs.com/tc310/archive/2012/12/21/2828450.html http://jingyan.baidu.com/artic ...

  5. Android Studio 3.1 Beta 1发布,如何及时下载更新

    每次收到Android Studio更新提示,总是延迟一段时间才能下载的到或者更新成功.架梯子也不行.而且更新检测也是时断时续.Android Studio 3.0.1使用一段时间,多开几个工程.经常 ...

  6. fir.im Weekly - iOS / Android 动态化更新方案盘点

    动态化更新是 App 开发必然面对的问题.在 iOS 环境下,Apple 开发者们像是" 带着手铐脚镣跳舞" ,相比之下 Android 开发者会轻松一点,有很多相关的开源框架帮助 ...

  7. Android 增量更新(BSDiff / bspatch)

    Android 增量更新 BSDiff / bspatchhttp://www.daemonology.net/bsdiff/android的代码目录下 \external\bsdiff bsdiff ...

  8. Android应用更新升级实现

    介绍 在产品的开发中,android升级提示,下载更新是必备的功能,否则等用户被动去官方网,或者第三方商店提示,就为时已晚了. 原理 在用户每次打开应用的时候,都与服务器进行一次交互,获取版本信息,对 ...

  9. android开发 更新升级安装到一半自动闪退

    如题:android开发 更新升级安装到一半自动闪退,,,解决办法,如下(红色为我新增的代码) /**     * 安装APK文件     */    private void installApk( ...

随机推荐

  1. onethink导出excel

    function customer_daochu() { /** * 客户名单导出 * */ $customer = D('WcoaUCustomers'); $data = $customer-&g ...

  2. Android源码编译出错解决办法

    编译环境:Ubuntu12.04 64位 Android源码:Android 4.3 以下问题是笔者亲自碰到,通过网上查询整合在一起的. 1.error while loading shared li ...

  3. 入门struts2.0

    框架是什么? 1.应用程序的半成品. 2.可重用行公共的结构. 3.按一定规则组织的一组组件. model2 其实并不是一种全新的概念,很对人指出model2其实正好是经典的"模型(mode ...

  4. Ruby Cucumber环境

    1.http://rubyinstaller.org/downloads 下载rubyinstaller以及developmentkit(注意版本号要对应) 2.安装rubyinstaller以及解压 ...

  5. Service Broker应用(2):不同server间的数据传输,包含集群

    不同Server之间的数据传输,包含DB使用AlwaysOn 配置脚本: SQL Server Service Broker 跨集群通信 具体的TSQL 脚本语句如下.注意的是TSQL语句是在发送方还 ...

  6. Angular JS的模块依赖

    AngularJS是纯客户端技术,完全用Javascript编写的.它使用的是网页开发的常规技术(HTML,CSS,Javascript),目的是让网页应用开发更快更容易. AngularJS简化应用 ...

  7. C语言学习1——结构体剖析

    一、定义结构体变量的方法 1.1先声明结构体类型在定义变量名 例如: a.声明结构体类型 struct student { int num; char name[20]; char sex; int ...

  8. 【转】 linux 下Time_wait过多问题解决

    问题起因: 自己开发了一个服务器和客户端,通过短连接的方式来进行通讯,由于过于频繁的创建连接,导致系统连接数量被占用,不能及时释放.看了一下18888,当时吓到了. 现象: 1.外部机器不能正常连接S ...

  9. ppp数据帧的格式

    参考http://blog.chinaunix.net/uid-11639156-id-2379044.html

  10. Github Atom 1.12.0-beta3 发布

    Github Atom 1.12.0-beta3 发布了,Atom 是 Github 专门为程序员推出的一个跨平台文本编辑器.具有简洁和直观的图形用户界面,并有很多有趣的特点:支持CSS,HTML,J ...