手机安全卫士开发系列(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;}}
<?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
- 手机安全卫士开发系列(3)——获取更新的服务器配置
- 手机安全卫士开发系列(4)——显示更新对话框
- 手机安全卫士开发系列(1)——功能列表
- 手机安全卫士开发系列(2)——splash界面
- 手机安全卫士开发系列(5)——自动更新
- 【边做项目边学Android】手机安全卫士03:获取更新的服务器配置,显示更新对话框
- 手机安全卫士开发系列(7)——知识点整理(1)
- 手机安全卫士开发系列(7)——知识点整理(2)
- 手机安全卫士开发系列(6)——程序主界面
- Android项目:手机安全卫士(15)—— 获取手机安装应用与存储空间
- 练手小项目(5)安全卫士——手机加速
- Android项目:手机安全卫士(2)—— 版本升级
- Android项目:手机安全卫士(8)—— 管理员权限
- Android项目:手机安全卫士(14)—— 短信备份
- Android项目:手机安全卫士(16)—— 复杂 ListView
- Android项目:手机安全卫士(3)—— 主界面布局
- 手机安全卫士1——splash
- Android手机安全卫士——01
- 在硬盘留下后门,重装系统都没辙(太太太太太厉害了)
- Reactor模式
- VB.NET多线程编程技术
- c语言基础
- nginx变量机制
- 手机安全卫士开发系列(3)——获取更新的服务器配置
- fill dropdownlist jquery
- 家具店-游泳馆-粥店 今天是这么过的... ...
- vim+tags+cscope+taglist
- 把数组排成最小的数
- hdu4671 构造
- json配置"db":true以后报错的解决方法
- poj 1832 连环锁
- NYOJ 题目17 单调递增最长子序列 (DP)