Android应用检查更新下载安装打开
2015-11-27 12:40
483 查看
一、效果
低版本1.02检测到新版本1.03
调用android的安装activity页面
安装完成
打开
1.03版本
二、注意
必须使用签名的应用。因为android不管是虚拟机还是真机安装应用都需要签名。在开发时,我们运行程序时,开发环境ADT会自动给我们加入一个默认的开发签名,所以我们不需要手动导出签名应用。
三、步骤
1、编写更新文件update.json2、编写更新代码
3、导出低版本签名应用(1.02)
4、adb命令安装低版本签名应用
5、导出高版本签名应用(1.03),放到tomcat服务器上
6、自动更新安装升级
四、各步骤详情
4.1编写更新文件update.json
{ "versonName":"1.03", "versonCode":3, "description":"新增了很多功能,更新体验吧!", "downloadUrl":"http://10.0.2.2:8080/android/ZMGMobileSafe1.03.apk" }
4.2编写更新代码
检查更新的功能/** *检查是否有新的版本 */ private void checkNewVersion(){ new Thread(){ public void run() { long startTime=System.currentTimeMillis(); //Android推荐使用,这个方法会先检测是否存在message实例,如果存在直接用,如果没有存在则new一个。 //Android系统默认有10个实例 Message msg=Message.obtain(); String url="http://10.0.2.2:8080/android/update.json"; try { //访问服务器获取json String result=HttpConnectionUtils.get(url); //解析json字符串 JSONObject js=new JSONObject(result); mVersonName = js.getString("versonName"); mVersonCode = js.getInt("versonCode"); mDescription = js.getString("description"); mDownloadUrl = js.getString("downloadUrl"); //比较服务器版本号和应用的版本号 if(mVersonCode>getVersionCode()){ msg.what=CODE_UPDATE_DIALOG; }else{ msg.what =CODE_ENTER_HOME; } } catch (MalformedURLException e) { //url格式错误 msg.what=CODE_URL_ERROR; e.printStackTrace(); } catch (IOException e) { //网络异常 msg.what=CODE_NET_ERROR; e.printStackTrace(); }catch (JSONException e) { //json格式错误 msg.what=CODE_JSON_ERROR; e.printStackTrace(); }finally{ //不能让闪屏页太快闪过去,强制等待 long endTime=System.currentTimeMillis(); long usedTime=endTime-startTime; if(usedTime<3000){ try { Thread.sleep(3000-usedTime); } catch (InterruptedException e) { e.printStackTrace(); } } mHandler.sendMessage(msg); } } }.start(); }
HttpConnectionUtils.java
/** * http-get的方式获取数据 * @param url 访问地址 * @return 字符串 * @throws MalformedURLException */ public static String get(String url) throws MalformedURLException,IOException{ String result=""; //MalformedURLException 不正确的url地址异常 URL connUrl=new URL(url); HttpURLConnection conn=(HttpURLConnection) connUrl.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000);//设置连接超时,如果5秒连不上,就不连接了,抛IOException conn.setReadTimeout(5000);//设置读取超时,虽然连接上了,但是服务器5秒了还不给反应,也就不等了,抛IOException conn.connect(); int responseCode=conn.getResponseCode(); if(responseCode==HttpURLConnection.HTTP_OK){ //读取响应头里的输入流 InputStream ins=conn.getInputStream(); result=StreamUtils.readFromStream(ins); } if(conn!=null){ conn.disconnect(); } return result; }
private void ShowUpdateDialog(){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle(getText(R.string.updateTitle)+mVersonName); builder.setMessage(mDescription); //设置当点击退回键的时候,对话框不关闭 builder.setCancelable(false); //设置确定按钮的点击事件 builder.setPositiveButton("立即升级", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { System.out.println("点击了立即升级"); downLoad(); } }); builder.setNegativeButton("以后再说", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { enterHome(); } }); //用户点击返回键时触发,点击了返回键则进入主界面 builder.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { enterHome(); } }); //这句话不能忘记哦,是把对话框显示出来 builder.show(); }
代码中用到了xUtils框架的httpUtils模块下载功能
private void downLoad() { //检查SD卡是否准备好了 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ tvProgress.setVisibility(View.VISIBLE);//显示进度条 String target=Environment.getExternalStorageDirectory()+"/safeUpdate.apk"; //xUtils HttpUtils utils=new HttpUtils(); utils.download(mDownloadUrl, target, new RequestCallBack<File>() { @Override public void onLoading(long total, long current, boolean isUploading) { super.onLoading(total, current, isUploading); tvProgress.setText("下载进度:"+current/total*100+"%"); } @Override public void onSuccess(ResponseInfo<File> responseInfo) { /* * 调用系统的安装activity <activity android:name=".PackageInstallerActivity" android:configChanges="orientation|keyboardHidden" android:theme="@style/Theme.Transparent"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="content" /> <data android:scheme="file" /> <data android:mimeType="application/vnd.android.package-archive" /> </intent-filter> </activity> */ Intent intent=new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setDataAndType(Uri.fromFile(responseInfo.result), "application/vnd.android.package-archive"); //startActivity(intent); //当用户点击了取消安装,会返回结果,回调方法onActivityResult startActivityForResult(intent, 0); //安装完成后,提示完成、打开 android.os.Process.killProcess(android.os.Process.myPid()); } @Override public void onFailure(HttpException error, String msg) { Toast.makeText(SplashActivity.this, "下载失败!", Toast.LENGTH_SHORT).show(); } }); }else{ Toast.makeText(this, "没有找到SD卡", Toast.LENGTH_SHORT).show(); } }
下载完成了安装时,调用android系统的activity页面,会显示安装页面,有取消和安装两个按钮。
/* * 调用系统的安装activity <activity android:name=".PackageInstallerActivity" android:configChanges="orientation|keyboardHidden" android:theme="@style/Theme.Transparent"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="content" /> <data android:scheme="file" /> <data android:mimeType="application/vnd.android.package-archive" /> </intent-filter> </activity> */ Intent intent=new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setDataAndType(Uri.fromFile(responseInfo.result), "application/vnd.android.package-archive"); //startActivity(intent); //当用户点击了取消安装,会返回结果,回调方法onActivityResult startActivityForResult(intent, 0); //安装完成后,提示完成、打开 android.os.Process.killProcess(android.os.Process.myPid());
4.3,4 导出签名应用
修改AndroidManifest.xml<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zengmg.MobileSafe" android:versionCode="2" android:versionName="1.02">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zengmg.MobileSafe" android:versionCode="3" android:versionName="1.03">
具体导出步骤见博文:/article/10292558.html
4.6 安装
如果报错可能是签名不一致问题
见博文:/article/10292556.html
相关文章推荐
- [Android]修改bitmap大小
- Android Studio 2.0 Instant Run即时运行功能实现分析
- Android 键盘使用二(监听输入法搜索时防止两次加载)
- Android 万年历日期选择器
- Android代码混淆、第三方平台加固加密、渠道分发 完整教程
- Android 键盘使用一(监听弹出隐藏)
- Notification的使用(学习笔记)
- android-studio add jar
- android RadioGroup和RadioButton使用注意
- Waterfall
- 图片三级缓存+线程池队列
- android 自定义通知布局Notification,点击Notification导航切换回原Activity
- Android之WebRTC介绍
- android Google Map获取地理位置信息的方法
- android插件话和hotfix
- 高仿窠浦2.0自定义RadioButton
- AndroidL 源码编译 预置GMS包与第三方应用 系统属性管理
- Android PickerView的实现
- Android自定义通知布局Notification,点击Notification导航切换回原Activity
- Android进阶之广播