好啦,我们继续我们昨天的那个项目,昨天我们只完成了一个程序启动时的欢迎界面,但是提到了,启动那个界面时会进行版本的检查,所以今天我们就做那个版本的检查那一块。
既然要做版本检查,那肯定要有服务器啦,所以我们就用到tomcat啦,因为这个项目是Android, 所以我就不写那个服务器端的程序啦,我只用tomcat来进行一个简单的从服务器读取数据,然后更新程序。不会专门写一个服务器端来进行业务的处理。
好,废话不多说,可能有些人还没接触过Web方面的,所以我把tomcat的搭建也简单的说一下,大神可以直接跳到下面去。
首先,我们去tomcat官网上下载一个tomcat 下载地址
在这里选择是32位的还是64位的
下载下来后,安装也很简单,直接解压出来就行啦(前提是你的java环境要正确)
解压好之后,我们就可以去到它的bin目录下面,双击那个startup.bat文件啦
然后我们就会看到一个黑呼呼的界面
然后,我们去浏览器输入 http://localhost:8080/ 然后出现下面的界面,那就说明你的tomcat配置成功了
那么,我们的服务器配置好之后,我们就要用它来为我们的app做一些东西啦
首先,我们在tomcat的webapps目录下载新建一个文件夹叫Security(这个是个人喜欢的,因为我们app叫Security)
update.xml里面的内容
- <?xml version="1.0" encoding="utf-8"?>
- <update>
- <version>1.0</version>
- <description>这里写一些这个版本的特点</description>
- <apkurl>http://192.168.1.5:8080/Security/new.apk</apkurl>
- <!--这里的ip地址一定要写你服务器所在的电脑的ip地址,我们会在Security这个目录下面放置一下new.apk的,用来更新的-->
- </update>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
-
- <string name="serverUrl">http://192.168.1.5:8080/Security/update.xml</string>
-
- </resources>
- package com.xiaobin.security.domain;
- public class UpdateInfo
- {
- private String version;
- private String description;
- private String url;
-
- public String getVersion()
- {
- return version;
- }
- public void setVersion(String version)
- {
- this.version = version;
- }
- public String getDescription()
- {
- return description;
- }
- public void setDescription(String description)
- {
- this.description = description;
- }
- public String getUrl()
- {
- return url;
- }
- public void setUrl(String url)
- {
- this.url = url;
- }
-
- }
- package com.xiaobin.security.engine;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import android.content.Context;
- import com.xiaobin.security.domain.UpdateInfo;
- public class UpdateInfoService
- {
-
- private Context context;
-
- public UpdateInfoService(Context context)
- {
- this.context = context;
- }
-
- public UpdateInfo getUpdateInfo(int urlId) throws Exception
- {
- String path = context.getResources().getString(urlId);//拿到config.xml里面存放的地址
- URL url = new URL(path);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();//开启一个http链接
- httpURLConnection.setConnectTimeout(5000);//设置链接的超时时间,现在为5秒
- httpURLConnection.setRequestMethod("GET");//设置请求的方式
- InputStream is = httpURLConnection.getInputStream();//拿到一个输入流。里面包涵了update.xml的信息
- return UpdateInfoParser.getUpdateInfo(is);//解析xml
- }
- }
- package com.xiaobin.security.engine;
- import java.io.InputStream;
- import org.xmlpull.v1.XmlPullParser;
- import android.util.Xml;
- import com.xiaobin.security.domain.UpdateInfo;
- public class UpdateInfoParser
- {
-
- public static UpdateInfo getUpdateInfo(InputStream is) throws Exception
- {
- UpdateInfo info = new UpdateInfo();
- XmlPullParser xmlPullParser = Xml.newPullParser();
- xmlPullParser.setInput(is, "utf-8");
- int type = xmlPullParser.getEventType();
- while(type != XmlPullParser.END_DOCUMENT)
- {
- switch(type)
- {
- case XmlPullParser.START_TAG :
- if(xmlPullParser.getName().equals("version"))
- {
- info.setVersion(xmlPullParser.nextText());
- }
- else if(xmlPullParser.getName().equals("description"))
- {
- info.setDescription(xmlPullParser.nextText());
- }
- else if(xmlPullParser.getName().equals("apkurl"))
- {
- info.setUrl(xmlPullParser.nextText());
- }
- break;
-
- default :
- break;
- }
- type = xmlPullParser.next();
- }
- return info;
- }
- }
- package com.xiaobin.security.ui;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.os.Bundle;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.animation.AlphaAnimation;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.xiaobin.security.R;
- import com.xiaobin.security.domain.UpdateInfo;
- import com.xiaobin.security.engine.UpdateInfoService;
- public class SplashActivity extends Activity
- {
- private TextView tv_version;
- private LinearLayout ll;
-
- private UpdateInfo info;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.splash);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- tv_version = (TextView) findViewById(R.id.tv_splash_version);
- String version = getVersion();
- tv_version.setText("版本号 " + version);
-
- ll = (LinearLayout) findViewById(R.id.ll_splash_main);
- AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
- alphaAnimation.setDuration(2000);
- ll.startAnimation(alphaAnimation);
-
- if(isNeedUpdate(version))
- {
- showUpdateDialog();
- }
- }
-
- private void showUpdateDialog()
- {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setIcon(android.R.drawable.ic_dialog_info);
- builder.setTitle("升级提醒");
- builder.setMessage(info.getDescription());
- builder.setCancelable(false);
-
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
- {
-
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- // TODO Auto-generated method stub
-
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- // TODO Auto-generated method stub
-
- }
-
- });
- builder.create().show();
- }
- private boolean isNeedUpdate(String version)
- {
- UpdateInfoService updateInfoService = new UpdateInfoService(this);
- try
- {
- info = updateInfoService.getUpdateInfo(R.string.serverUrl);
- String v = info.getVersion();
- if(v.equals(version))
- {
- System.out.println("不用更新");
- return false;
- }
- else
- {
- System.out.println("要更新");
- return true;
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- Toast.makeText(this, "获取更新信息异常,请稍后再试", Toast.LENGTH_SHORT).show();
- }
- return false;
- }
- private String getVersion()
- {
- try
- {
- PackageManager packageManager = getPackageManager();
- PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
-
- return packageInfo.versionName;
- }
- catch (NameNotFoundException e)
- {
- e.printStackTrace();
- return "版本号未知";
- }
- }
- }
- <uses-permission android:name="android.permission.INTERNET"/>