检测APK版本有更新,手动升级安装
来源:互联网 发布:美女与野兽 知乎 编辑:程序博客网 时间:2024/06/02 01:26
直接上代码了,此处不难理解。有什么问题可以留言。
从服务器获取xml解析并进行比对版本号
class CheckVersionTask implements Runnable{ public void run() { try { //从资源文件获取服务器 地址 String update_url = ServerBiz.getServer(HomeActivity.this, "update_url"); //包装成url的对象 URL url = new URL(update_url); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); (此处报错,输出conn.getResponseCode()看看!) conn.setConnectTimeout(5000); InputStream is =conn.getInputStream(); //获得更新文件的信息 UpdateInfo info = DownLoadManager.getUpdateInfo(is); if(info.getVersion().equals(getVersionName())){ Log.i("liyongjun","版本号相同无需升级"); }else{ Log.i("liyongjun","版本号不同 ,提示用户升级 "); Intent intent = new Intent("com.ccfrom.cclive.ACTION.ApkUpdate"); intent.putExtra("updatemsg", info.getDescription()); intent.putExtra("filepath",info.getUrl()); sendBroadcast(intent); } } catch (Exception e) { // 待处理 Message msg = new Message(); msg.what = GET_UNDATAINFO_ERROR; handler.sendMessage(msg); e.printStackTrace(); } } }
获取package的版本号,配置在manifest中的
private String getVersionName() throws Exception{ //获取packagemanager的实例 PackageManager packageManager = getPackageManager(); //getPackageName()是你当前类的包名,0代表是获取版本信息 PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0); return packInfo.versionName; }
注册此接收器:
class ApkUpdateReciver extends BroadcastReceiver { public void onReceive(Context paramContext, Intent paramIntent) { String str1 = paramIntent.getAction(); if (str1.equals("com.ccfrom.cclive.ACTION.ApkUpdate")) { String updatemsg = paramIntent.getStringExtra("updatemsg"); String filepath = paramIntent.getStringExtra("filepath"); HomeActivity.this.createUpdateDialog(updatemsg, filepath).show(); HomeActivity.this.removeStickyBroadcast(paramIntent); } } }
创建提示框,是否更新版本
public AlertDialog createUpdateDialog(String updatemsg, final String filepath) { AlertDialog.Builder builer = new Builder(this) ; builer.setTitle("版本升级"); builer.setMessage(updatemsg); builer.setPositiveButton("确定", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { downLoadApk(filepath); } }); builer.setNegativeButton("取消", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { Log.e("HomeActivity", "Enter Main.."); } }); return builer.create(); }
从服务器中下载apk
protected void downLoadApk(final String filePath) { pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage("正在下载更新"); pd.show(); new Thread(){ @Override public void run() { try { File file = DownLoadManager.getFileFromServer(filePath, pd); sleep(3000); installApk(file); pd.dismiss(); //结束掉进度条对话框 } catch (Exception e) { e.printStackTrace(); Message msg = new Message(); msg.what = DOWN_ERROR; handler.sendMessage(msg); } }}.start(); }
安装已下载的apk,并结束当前应用
protected void installApk(File file) { Intent intent = new Intent(); //执行动作 intent.setAction(Intent.ACTION_VIEW); //执行的数据类型 intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent); }
以下贴上前面需要使用到的类方法:
ServerBiz.java
private static ServerInfo parserServerXml(String paramString) { ServerInfo mServerInfo = null; if (paramString != null) { mServerInfo = new ServerInfo(); try { DocumentBuilderFactory domfac = DocumentBuilderFactory .newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); Document doc; Element root; NodeList maps; URL url = new URL(paramString); doc = dombuilder.parse(url.openStream()); root = doc.getDocumentElement(); maps = root.getChildNodes(); for (Node node = maps.item(1).getFirstChild(); node != null; node = node .getNextSibling()) { if (node.getNodeType() == Node.ELEMENT_NODE) { Log.e("liyongjun","name = " + node.getNodeName()); if (node.getNodeName().equals("hosturl")) mServerInfo.hosturl = node.getTextContent(); else if (node.getNodeName().equals("liveurl")) mServerInfo.liveurl = node.getTextContent(); else if (node.getNodeName().equals("mv_url")) mServerInfo.mv_url = node.getTextContent(); else if (node.getNodeName().equals("news_url")) mServerInfo.news_url = node.getTextContent(); else if (node.getNodeName().equals("playurl")) mServerInfo.playurl = node.getTextContent(); else if (node.getNodeName().equals("tvback_url")) mServerInfo.tvback_url = node.getTextContent(); else if (node.getNodeName().equals("tvepg_url")) mServerInfo.tvepg_url = node.getTextContent(); else if (node.getNodeName().equals("update_url")) mServerInfo.update_url = node.getTextContent(); } } } catch (Exception e) { e.printStackTrace(); } } return mServerInfo; } private static void saveServerInfo(Context paramContext, ServerInfo paramServerInfo) { SharedPreferences.Editor localEditor = paramContext .getSharedPreferences("server_preferences", paramContext.MODE_PRIVATE).edit(); localEditor.putString("hosturl", paramServerInfo.hosturl); localEditor.putString("liveurl", paramServerInfo.liveurl); localEditor.putString("mv_url", paramServerInfo.mv_url); localEditor.putString("news_url", paramServerInfo.news_url); localEditor.putString("playurl", paramServerInfo.playurl); localEditor.putString("tvback_url", paramServerInfo.tvback_url); localEditor.putString("tvepg_url", paramServerInfo.tvepg_url); localEditor.putString("update_url", paramServerInfo.update_url); localEditor.commit(); } public static String getServer(Context paramContext, String paramString) { return paramContext.getSharedPreferences("server_preferences", paramContext.MODE_PRIVATE).getString(paramString, null); }
UpdateInfo.java
private String version; private String url; private String description; public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; }
update.xml 此文件放于服务器指定位置,用来比较当前版本的版本号
<?xml version="1.0" encoding="utf-8"?><info> <version>2.0</version> <url>http://ccfrom.qtcms.cn/files/CcLive.apk</url> <description>检测到最新版本,请及时更新!</description> </info>
0 0
- 检测APK版本有更新,手动升级安装
- 版本检测升级(更新)库,集成了检测版本、下载版本以及自动安装升级
- Android版本检测,更新,安装及运行APK
- Android版本检测更新升级
- Android apk版本检测及更新
- 更新APK版本-便捷安装
- apk版本更新下载安装
- apk下载并安装、断点续传、升级检测
- android apk版本自动检测升级,安装
- android apk版本自动检测升级,安装
- Python Scrapy安装(python 版本手动更新)
- AS版本手动升级
- Apk安装过程中闪退 版本更新 Apk闪退
- Android版本更新知识(检测、升级)总结
- Android版本更新知识(检测、升级)总结
- APK升级更新,打包
- [android] apk 版本升级
- Android开发必有功能,更新版本提示,检测是否有新版本更新。下载完成后进行安装。
- android 中envsetup 输出的全局变量
- 编译linux内核
- Android EditText 自动弹出软键盘,遮挡输入框
- 黑马程序员_浅谈Java当中的异常处理机制
- Selenium寻找元素定位 id findElement(by.id(“id”)) name findElement(by.name(“name”)) class_name findE
- 检测APK版本有更新,手动升级安装
- 如何从ArcMap外与ArcMap交互
- OpenCV人脸识别的原理 .
- ATL COM中IDL文件里定义函数有默认值的参数的写法
- JAVA中Array的常用方法
- WebDriver拾级而上·之十六 Table控件的处理
- 东阳用友软件,东阳用友财务软件,东阳用友进销存,东阳0A,东阳HR,东阳ERP,东阳PDM,东阳CRM,东阳PLM,东阳U8,东阳U9,东阳NC,东阳T+,东阳BQ,东阳T3,东阳T6,东阳用友年度结
- Eclipse导入Android项目
- 基于经纬度的简单附近地理位置搜索