手机安全卫士开发系列(3)——获取更新的服务器配置

来源:互联网 发布:windows xp pe 最小 编辑:程序博客网 时间:2024/06/06 03:24

一、改变应用程序图标

在mainfest文件中有这么一段内容:

<uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".ui.SplashActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>

其中的<application>节点下的icon是整个应用程序的图标。

而<activity>节点下的icon是显示到luncher界面的图标。

二、连接服务器

主要流程如图:


首先解压tomcat


然后配置tomcat环境变量


启动tomcat并访问http://localhost:8080


在tomcat的webapp/ROOT目录下创建文件update.xml


update.xml

<?xml version="1.0" encoding="utf-8"?><info><version>2.0</version><description>ðӭµİ汾</description><apkurl>http://localhost:8080/newapk.apk</apkurl></info>

然后访问 http://locahost:8080/update.xml

为什么会出现这个错误呢?

这是因为xml的编码格式是utf-8,而编辑器对他不是以utf-8编码的,解决如下:


存放信息的实体类

package com.meritit.mobiesafe.domain;public class UpdateInfo {private String version;private String description;private String apkurl;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 getApkurl() {return apkurl;}public void setApkurl(String apkurl) {this.apkurl = apkurl;}}

从服务器获取信息

/** *  * @param is 服务器文件流 * @return   更新信息 * @throws Exception  */public UpdateInfo getUpdateInfo(int urlId) throws Exception{String path = context.getResources().getString(urlId);URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");InputStream is = conn.getInputStream();//解析xml文件return UpdateInfoParser.getUpdateInfo(is);}

解析xml工具类

package com.meritit.mobiesafe.engine;import java.io.InputStream;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import android.util.Xml;import com.meritit.mobiesafe.domain.UpdateInfo;public class UpdateInfoParser {public static UpdateInfo getUpdateInfo(InputStream is) throws Exception{XmlPullParser parser = Xml.newPullParser();UpdateInfo info = new UpdateInfo();parser.setInput(is, "utf-8");//定位到xml文件开头int type = parser.getEventType();while(type != XmlPullParser.END_DOCUMENT){switch (type) {case XmlPullParser.START_TAG:if("version".equals(parser.getName())){info.setVersion(parser.nextText());}else if("description".equals(parser.getName())){info.setDescription(parser.nextText());}else if("apkurl".equals(parser.getName())){info.setApkurl(parser.nextText());}break;}type = parser.next();}return info;}}


mainfest中配置用于测试的信息

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.meritit.mobiesafe"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <instrumentation        android:name="android.test.InstrumentationTestRunner"        android:targetPackage="com.meritit.mobiesafe" />    <uses-permission android:name="android.permission.INTERNET"/>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme"         >        <uses-library android:name="android.test.runner" />        <activity            android:name=".ui.SplashActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

测试类

package com.meritit.mobiesafe.test;import com.meritit.mobiesafe.R;import com.meritit.mobiesafe.domain.UpdateInfo;import com.meritit.mobiesafe.engine.UpdateInfoService;import android.test.AndroidTestCase;public class TestGetUpdateInfo extends AndroidTestCase {public void testGetInfo()throws Exception{UpdateInfoService service = new UpdateInfoService(getContext());UpdateInfo info = service.getUpdateInfo(R.string.updateurl);System.out.println(info.getVersion());assertEquals("2.0", info.getVersion());}}

测试结果


源代码下载:http://download.csdn.net/detail/lxq_xsyu/5929515