Android 版本更新 请求Web 服务器
来源:互联网 发布:VM怎样全屏在Linux下 编辑:程序博客网 时间:2024/06/06 07:32
.版本升级 就是拿到清单文件中android:versionCode="1"个服务器versionServer 进行对比,versionServer >android:versionCode 升级,拿到返回的apk下载链接,安装。
1.拿到本地versionCode
//获取版本号public static int getVersion(Context context){int version = 0;try { version = context.getPackageManager().getPackageInfo( "你项目的包名", 0).versionCode; //包名对应AndroidManifest.xm package="……" } catch (Exception e) { System.out.println("获取版本号异常!"); } return version;}
2.拿到本地传到服务器 有服务器判断 返回下载链接
3.开启子线程进行网络请求
//版本升级 String DEVICE_ID=null;//唯一标识码int version;String Strversion=null;//版本号转StringThread serviceThread; AlertDialog.Builder adb;private String Downurl=null;//下载链接
serviceThread= new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubString namespace ="http://tempuri.org/";//targetNamespace="http://tempuri.org/">String transUrl ="";//接口String method ="VersionUpgrade";//s:element name="VersionUpgrade">//后台询问或者从wsdl文档或者服务说明中查看int envolopeVersion =SoapEnvelope.VER12;//SoapObject request=new SoapObject(namespace, method); Log.i("TAG", "版本号version====="+Strversion +"识别码DEVICE_ID====="+DEVICE_ID); request.addProperty("versionNo", version); request.addProperty("machineCode", Strversion);//本机唯一识别码 不需要可以不传 SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(envolopeVersion); envelope.setOutputSoapObject(request);envelope.dotNet=true;HttpTransportSE httpse=new HttpTransportSE(transUrl);try {httpse.call(null, envelope);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (XmlPullParserException e) {// TODO Auto-generated catch blocke.printStackTrace();}Object response;try { response = (Object) envelope.getResponse();if (response!=null) {//如果返回不为bull 则可以升级,如果是null 则不需要更新SoapObject result = (SoapObject) envelope.bodyIn; Downurl = result.getProperty(0).toString(); Log.i("TAG", "response"+response); Log.i("TAG", "下载链接Downurl"+Downurl); handler.sendEmptyMessage(1);}else { AlertDialog.Builder builder = new Builder(getActivity()); builder.setTitle("版本信息" ) ; builder.setMessage("当前已经是最新版本" ) ; builder.setPositiveButton("确定",null); builder.show();Log.i("TAG", "无返回值");Toast.makeText(getActivity(), "无最新版本", Toast.LENGTH_LONG).show();}} catch (SoapFault e) {// TODO Auto-generated catch blocke.printStackTrace();}}});
4.得到下载链接后
hander 发消息
handler.sendEmptyMessage(1);
5.开启handle
private Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case 1: showUpdateDialog();是否更新Dialog Log.i("TAG", "case 1:============case 1:"); break;}}};
private void showUpdateDialog() { //弹出对话框提示更新adb = new AlertDialog.Builder(getActivity()); adb.setTitle("发现新版本"); adb.setCancelable(false);//要么点击确定,要么点击取消。否则不会关闭dialog adb.setNegativeButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //下载更新的APK downUpdateAPK(); } }); adb.setPositiveButton("取消",null); adb.show();}6.得到 Url 下载 就好比从服务器下载图片一样了
6.1判断是否有sd卡
String storageState = Environment.getExternalStorageState(); if (storageState.equals(Environment.MEDIA_MOUNTED)) { savePath = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/update/"; File file = new File(savePath); if (!file.exists()) { file.mkdir(); } apkFilePath = savePath + apkName; tmpFilePath = savePath + tmpApk; Log.i("apkUrl","tmpFilePath"+tmpFilePath ); }
6.2 开启线程 下载
downloadThread = new Thread(mdownApkRunnable);
private Runnable mdownApkRunnable = new Runnable() { @Override public void run() { try { String apkName = "AccessControllSystem" + ".apk"; String tmpApk = "AccessControllSystem" + ".tmp"; // 判断是否挂载了sd卡 String storageState = Environment.getExternalStorageState(); if (storageState.equals(Environment.MEDIA_MOUNTED)) { savePath = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/update/"; File file = new File(savePath); if (!file.exists()) { file.mkdir(); } apkFilePath = savePath + apkName; tmpFilePath = savePath + tmpApk; Log.i("apkUrl","tmpFilePath"+tmpFilePath ); } // 没有挂载sd卡,无法下载 if (apkFilePath == null || apkFilePath == "") { mhandler.sendEmptyMessage(DOWN_NOSDCARD); return; } File ApkFile = new File(apkFilePath); // 输出临时文件 File tempFile = new File(tmpFilePath); FileOutputStream fos = new FileOutputStream(tempFile); URL url = new URL(apkUrl); // 定义url链接对象 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); // 建立链接 int length = conn.getContentLength(); // 得到链接内容的 InputStream is = conn.getInputStream(); DecimalFormat df = new DecimalFormat("0.00"); apkFileSize = df.format((float) length / 1024 / 1024) + "MB"; int count = 0; byte buf[] = new byte[1024]; // 循环下载 do { int numread = is.read(buf); count += numread; tmpFileSize = df.format((float) count / 1024 / 1024) + "MB"; progress = (int) (((float) count / length) * 100); Log.i("TAG", "tmpFileSize"+tmpFileSize); mhandler.sendEmptyMessage(DOWN_UPDATE); if (numread <= 0) { if (tempFile.renameTo(ApkFile)) { // 下载完成 mhandler.sendEmptyMessage(DOWN_OVER); } break; } fos.write(buf, 0, numread); } while (!interceptFlag); fos.close(); is.close(); } catch (Exception ex) { ex.printStackTrace(); } } };6.4安装
private void installApk() { File apkfile = new File(apkFilePath); if (!apkfile.exists()) { return; } // 如果存在,启动安装流程 Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse("file://" + apkfile.toString()),"application/vnd.android.package-archive"); context.startActivity(i); }
1.启动网络webservice 通过SOAP(是web service的标准通信协议,SOAP为simple object access protocoll的缩写,简单对象访问协议. 它是一种标准化的传输消息的XML消息格式).需要导jar: ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar
2.开启线程
0 0
- Android 版本更新 请求Web 服务器
- android 应用版本更新,自己的服务器!
- Android手机访问web服务器(post请求)
- Android中版本更新(使用HttpURLConnection网络请求)
- 版本更新的请求网址
- Node.js实现android的apk版本更新服务器
- Android----- 版本更新和 服务器下载新版本APK并安装
- Windows Web 服务器版本
- Android向Web服务器发送Post请求并返回数据
- Android向Web服务器发送Post请求并返回数据
- Android版Web服务器实现(四)界面更新
- Android版本更新
- Android SDK版本更新
- Android软件版本更新
- Android 检测版本更新
- Android之版本更新
- Android版本更新实现
- android版本更新问题
- 【《Real-Time Rendering 3rd》 提炼总结】(一) 全书知识点总览
- Android studio github登录时 Can't login: Can't get user info Bad Request
- Android 插值器 Interpolator 介绍
- 深入理解JVM(一)-理解内存分配
- 母牛的故事
- Android 版本更新 请求Web 服务器
- linux sed命令学习
- HDU 1429 胜利大逃亡(续)
- 【Java之IO】7.数据流
- 云主机搭建个人网站
- MySQL 之 set global sql_slave_skip_counter=N 命令
- DeepMind发布Sonnet 帮你用TensorFlow快速搭建神经网络
- HDU 1029Ignatius and the Princess IV(基础dp(思维))
- Retrofit带参上传图片相关问题