Android下载更新代码
2016-10-13 11:18
309 查看
其实是昨天反编译一个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===========
相关文章推荐
- 【Ionic】Ionic实现iOS与Android端代码『热更新』与Android升级下载功能
- Android 增量更新(更新时只需客户端下载新加的功能代码)
- 【Ionic】Ionic实现iOS与Android端代码『热更新』与Android升级下载功能 ( v1.3.x版本 )
- Android APK 版本更新的下载和安装 直接上代码
- 安卓自动更新,静默更新,替换友盟更新可以在通知栏里显示更新下载进度,几行代码快速实现Android下载更新
- Android App更新版本以及提示用户下载最新apk(有图,有代码,有最终效果图)详细解释及思路
- FormValidate 表单验证功能代码更新并提供下载
- 生成/读取(反向更新数据库) Excel文件(示例代码下载)
- C#下载文件代码更新20070920
- [转]生成/读取(反向更新数据库) Excel文件(示例代码下载)
- Android开源项目源码下载(不断更新中)
- AutoCode (vs插件)自动生成代码插件 下载及使用教程(C#版)( 更新简单自定义模板教程)
- FormValidate 表单验证功能代码更新并提供下载
- android中程序更新下载完成后自动跳转安装界面
- 更新Eclipse,下载ADT(Android开发插件)
- Ubuntu 10.04环境下载&编译Android-2.1_r2源代码【20110527更新说明】
- [转] 生成/读取(反向更新数据库) Excel文件(示例代码下载)
- C#下载文件代码更新20070920
- 使用PB调用API自动更新(非FTP模式)(七、下载代码)
- WebGame《逆转裁判》完整版 代码下载(1月24日更新)