第一步,SplashActivity
1.获取用户客户端版本号,注意manifest里去掉title和权限
layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" android:background="@drawable/bg"> <TextView android:id="@+id/tv_splash_version" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:textColor="#000000" android:textSize="16sp" android:layout_marginTop="120dp" android:shadowDx="1" android:shadowDy="1" android:shadowRadius="2" android:shadowColor="#FF0000"/> </RelativeLayout>
activity:
package com.rjl.mobilephonemanager; import android.app.Activity; import android.os.Bundle; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.widget.TextView; public class SplashActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); TextView tv_splash_version = (TextView) findViewById(R.id.tv_splash_version); //上面的声明要注意是空字符串,而不是null tv_splash_version.setText("版本: "+getVersion()); } private String getVersion() { //获取版本号 PackageManager pm = getPackageManager(); PackageInfo packageinfo; String version=""; try { //第一个参数应该为manifest里的包名而不是工程里的包名,后直接以方法获取~ packageinfo = pm.getPackageInfo(getPackageName(), 0); version = packageinfo.versionName; } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return version; } }
2.获取服务器端最新版本号,假设已经是3.0版,JSON解析
当有可升级版本时,info文件里肯定还是有相关描述和下载提示的
在oncreate里调用update方法,在后面实现这个方法,这个方法需要在子线程里实现
private void Update() { // TODO Auto-generated method stub new Thread(new Runnable(){ @Override public void run() { String path = "http://192.168.3.49/version_info.json"; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.setConnectTimeout(2000); //实际发布版本的时候,超时实际最好稍微长一些 conn.setRequestMethod("GET"); conn.connect(); if (conn.getResponseCode()==200) { } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); }
现在来完成if部分
当响应码为200时,转换流,接收,解析,此时用到JSON解析,这一部分可以抽成一个工具,之前写了一个,拿过来用
utils工具
package utils; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class WebUtils { public static String gettextFromInputStream(InputStream is ,String charset){ String text = null; if(charset==null){ charset="utf-8"; } try { ByteArrayOutputStream baos= new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len = 0; while ((len=is.read(b))!=-1) { baos.write(b, 0, len); } baos.close(); text = new String(baos.toByteArray(), charset); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return text; } }
获取相关版本信息并打印trace看看,需要启动服务器
if (conn.getResponseCode()==200) { InputStream is= conn.getInputStream(); String jsontext = WebUtils.gettextFromInputStream(is, null); JSONObject jsonObject = new JSONObject(jsontext); version = jsonObject.getString("version"); System.out.println("SplashActivity.Update().new Runnable() {...}.run()" +version); }
要注意编码的问题,安卓,还有utils里的编码都是utf-8,这里服务器端的info文件如果不是这个编码需要改过来,还有json里是英文状态的标点,引号内无所谓
在实际操作过程中,肯定需要判断下版本是否一致
if (conn.getResponseCode()==200) { InputStream is= conn.getInputStream(); String jsontext = WebUtils.gettextFromInputStream(is, null); JSONObject jsonObject = new JSONObject(jsontext); version = jsonObject.getString("version"); System.out.println("SplashActivity.Update().new Runnable() {...}.run()" +version); if(version.equals(getVersion())){ //版本一致则进入主页 }else{ //不一致则弹出dialog告诉用户,此时需要handler来处理 } }
注意里面实现的方法是handleMessage,而不是handlerMessage
Handler myHandler = new Handler(){ public void handleMessage(android.os.Message msg){ switch(msg.what){ case MSG_VERSION_SAME: //进入主界面 break; case MSG_VERSION_DIFF: //弹出通知 break; default: break; } }; };
if(version.equals(getVersion())){ //版本一致则进入主页 Message msg = myHandler.obtainMessage(); msg.what = MSG_VERSION_SAME; myHandler.sendMessage(msg); }else{ //不一致则弹出dialog告诉用户,此时需要handler来处理 Message msg = myHandler.obtainMessage(); msg.what = MSG_VERSION_DIFF; myHandler.sendMessage(msg); } }
可升级时,服务器端必须给出提示
if (conn.getResponseCode()==200) { InputStream is= conn.getInputStream(); String jsontext = WebUtils.gettextFromInputStream(is, null); JSONObject jsonObject = new JSONObject(jsontext); version = jsonObject.getString("version"); description = jsonObject.getString("description"); downloadurl = jsonObject.getString("downloadurl"); System.out.println("SplashActivity.Update().new Runnable() {...}.run()" +version +description +downloadurl);
case MSG_VERSION_DIFF: //弹出通知 Toast.makeText(SplashActivity.this, "有新版本", 1).show(); break;
至此,对比用户端和服务器端的工作完成,下一步可以实现更新下载等操作
这一段的完整代码
import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.json.JSONException; import org.json.JSONObject; import utils.WebUtils; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.widget.TextView; import android.widget.Toast; public class SplashActivity extends Activity { protected static final int MSG_VERSION_SAME =0; protected static final int MSG_VERSION_DIFF =1; private String description; private String downloadurl; private String version; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); TextView tv_splash_version = (TextView) findViewById(R.id.tv_splash_version); //上面的声明要注意是空字符串,而不是null tv_splash_version.setText("版本: "+getVersion()); //获取服务器端版本号 Update(); } Handler myHandler = new Handler(){ public void handleMessage(android.os.Message msg){ switch(msg.what){ case MSG_VERSION_SAME: //进入主界面 break; case MSG_VERSION_DIFF: //弹出通知 Toast.makeText(SplashActivity.this, "有新版本", 1).show(); break; default: break; } }; }; private void Update() { // TODO Auto-generated method stub new Thread(new Runnable(){ @Override public void run() { String path = "http://192.168.3.49/version_info.json"; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.setConnectTimeout(2000); //实际发布版本的时候,超时实际最好稍微长一些 conn.setRequestMethod("GET"); conn.connect(); if (conn.getResponseCode()==200) { InputStream is= conn.getInputStream(); String jsontext = WebUtils.gettextFromInputStream(is, null); JSONObject jsonObject = new JSONObject(jsontext); version = jsonObject.getString("version"); description = jsonObject.getString("description"); downloadurl = jsonObject.getString("downloadurl"); System.out .println("SplashActivity.Update().new Runnable() {...}.run()" +version +description +downloadurl); if(version.equals(getVersion())){ //版本一致则进入主页 Message msg = myHandler.obtainMessage(); msg.what = MSG_VERSION_SAME; myHandler.sendMessage(msg); }else{ //不一致则弹出dialog告诉用户,此时需要handler来处理 Message msg = myHandler.obtainMessage(); msg.what = MSG_VERSION_DIFF; myHandler.sendMessage(msg); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } private String getVersion() { //获取版本号 PackageManager pm = getPackageManager(); PackageInfo packageinfo; String version=""; try { //第一个参数应该为manifest里的包名而不是工程里的包名,后直接以方法获取~ packageinfo = pm.getPackageInfo(getPackageName(), 0); version = packageinfo.versionName; } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return version; } }