您的位置:首页 > 编程语言

代码不执行问题?费解,求解

2013-11-29 09:43 169 查看
为了实现一个自动更新的功能,但是在实现的过程中遇到了一些奇怪的问题,暂时性还没有解决,请大家帮忙看看。

问题是这样的,我自定义了两个类,用于实现ap的版本与管理控制,以及下载.

<update>

<version>3</version> <name>yitu_1.0</name> <url>http://222222/yitu/Upload/yitu_old.apk</url></update>

使用代码如下:



日志代码:



实现效果:



我按照要求进行了更新,原先的版本配置文件也没有更换,并且在服务器端的版本也没有更换,即实际的程序版本还是原来的。



奇怪的事发生了,一切更新完毕,我再次打开这个应用时.



请大家有时间帮忙看看,万分感谢。或者wsfjlagr@163.com

UpdateManager .java

1 public class UpdateManager {
2 /* 下载中 */
3 private static final int DOWNLOAD = 1;
4 /* 下载结束 */
5 private static final int DOWNLOAD_FINISH = 2;
6 /* 保存解析的XML信息 */
7 HashMap<String, String> mHashMap;
8 /* 下载保存路径 */
9 private String mSavePath;
10 /* 记录进度条数量 */
11 private int progress;
12 /* 是否取消更新 */
13 private boolean cancelUpdate = false;
14
15 private Context mContext;
16 /* 更新进度条 */
17 private ProgressBar mProgress;
18
19 private Dialog mDownloadDialog;
20
21 private String soft_update_no = "当前是最新版本";
22
23 private String soft_update_title = "软件更新";
24
25 private String soft_update_info = "更新消息";
26
27 private String soft_update_updatebtn = "更新";
28
29 private String soft_update_later = "稍后更新";
30
31 private String soft_updating = "正在更新新版本";
32
33 private String soft_update_cancel = "取消更新";
34
35 private String request_path="http://*********/yitu/Upload/appversion.txt";
36
37 private String packagename="com.df.dianping";
38 private Handler mHandler = new Handler() {
39 public void handleMessage(Message msg) {
40 switch (msg.what) {
41 // 正在下载
42 case DOWNLOAD:
43 // 设置进度条位置
44 mProgress.setProgress(progress);
45 break;
46 case DOWNLOAD_FINISH:
47 // 安装文件
48 installApk();
49 break;
50 default:
51 break;
52 }
53 };
54 };
55
56 public UpdateManager(Context context) {
57 this.mContext = context;
58 }
59
60 private Handler handler=new Handler(new Handler.Callback() {
61
62 @Override
63 public boolean handleMessage(Message msg) {
64 // TODO Auto-generated method stub
65 switch(msg.what)
66 {
67
68
69 case 0:
70 showNoticeDialog();
71 break;
72 }
73
74 return false;
75 }
76 });
77
78 /**
79 * 检测软件更新
80 */
81 public void checkUpdate() {
82
83 Log.i("update", "开始更新");
84
85 new Thread(new Runnable() {
86
87 @Override
88 public void run() {
89 // TODO Auto-generated method stub
90 sendGetResquest();
91 }
92 }).start();
93 }
94 /*
95 * Function : 发送GET请求
96 * Author : 博客园-依旧淡然
97 */
98 public void sendGetResquest() {
99 Log.i("net", "begian check_version");
100 HttpGet httpGet = new HttpGet(request_path); //创建一个GET方式的HttpRequest对象
101 DefaultHttpClient httpClient = new DefaultHttpClient(); //创建一个默认的HTTP客户端
102 try {
103 HttpResponse httpResponse = httpClient.execute(httpGet); //执行GET方式的HTTP请求
104 int reponseCode = httpResponse.getStatusLine().getStatusCode(); //获得服务器的响应码
105 if(reponseCode == HttpStatus.SC_OK) {
106 // 把version.xml放到网络上,然后获取文件信息
107 InputStream inStream = httpResponse.getEntity().getContent(); //获得服务器的响应内容
108
109 // 获取当前软件版本
110 int versionCode = getVersionCode(mContext);
111
112 // 解析XML文件。 由于XML文件比较小,因此使用DOM方式进行解析
113 ParseXmlService service = new ParseXmlService();
114 try {
115 mHashMap = service.parseXml(inStream);
116 } catch (Exception e) {
117 e.printStackTrace();
118 }
119 Log.i("net", "null != mHashMap"+(null != mHashMap));
120 if (null != mHashMap) {
121 int serviceCode = Integer.valueOf(mHashMap.get("version"));
122 // 版本判断
123 Log.i("net", "serview_code-->"+serviceCode+" versionCode"+versionCode);
124 if (serviceCode > versionCode) {
125 // 显示提示对话框
126 Message msg=new Message();
127 msg.what=0;
128 handler.sendMessage(msg);
129 }
130 }
131 inStream.close();
132 }
133 } catch (ClientProtocolException e) {
134 e.printStackTrace();
135 } catch (IOException e) {
136 e.printStackTrace();
137 }
138 }
139
140 /**
141 * 获取软件版本号
142 *
143 * @param context
144 * @return
145 */
146 private int getVersionCode(Context context) {
147 int versionCode = 0;
148 try {
149 // 获取软件版本号,对应AndroidManifest.xml下android:versionCode
150 versionCode = context.getPackageManager().getPackageInfo(
151 packagename, 0).versionCode;
152 } catch (NameNotFoundException e) {
153 e.printStackTrace();
154 }
155 return versionCode;
156 }
157
158 /**
159 * 显示软件更新对话框
160 */
161 private void showNoticeDialog() {
162 // 构造对话框
163 AlertDialog.Builder builder = new Builder(mContext);
164 builder.setTitle(soft_update_title);
165 builder.setMessage(soft_update_info);
166 // 更新
167 builder.setPositiveButton(soft_update_updatebtn, new OnClickListener() {
168 @Override
169 public void onClick(DialogInterface dialog, int which) {
170 dialog.dismiss();
171 // 显示下载对话框
172 showDownloadDialog();
173 }
174
175 public void onClick(View v) {
176 // TODO Auto-generated method stub
177
178 }
179 });
180 // 稍后更新
181 builder.setNegativeButton(soft_update_later, new OnClickListener() {
182 @Override
183 public void onClick(DialogInterface dialog, int which) {
184 dialog.dismiss();
185 }
186 });
187 Dialog noticeDialog = builder.create();
188 noticeDialog.show();
189 }
190
191 /**
192 * 显示软件下载对话框
193 */
194 private void showDownloadDialog() {
195 // 构造软件下载对话框
196 AlertDialog.Builder builder = new Builder(mContext);
197 builder.setTitle(soft_updating);
198 // 给下载对话框增加进度条
199 final LayoutInflater inflater = LayoutInflater.from(mContext);
200 View v = inflater.inflate(R.layout.softupdate_progress, null);
201 mProgress = (ProgressBar) v.findViewById(R.id.update_progress);
202 builder.setView(v);
203 // 取消更新
204 builder.setNegativeButton(soft_update_cancel, new OnClickListener() {
205 public void onClick(DialogInterface dialog, int which) {
206 dialog.dismiss();
207 // 设置取消状态
208 cancelUpdate = true;
209 }
210 });
211 mDownloadDialog = builder.create();
212 mDownloadDialog.show();
213 // 现在文件
214 downloadApk();
215 }
216
217 /**
218 * 下载apk文件
219 */
220 private void downloadApk() {
221 // 启动新线程下载软件
222 new downloadApkThread().start();
223 }
224
225 /**
226 * 下载文件线程
227 *
228 * @author coolszy
229 * @date 2012-4-26
230 * @blog http://blog.92coding.com 231 */
232 private class downloadApkThread extends Thread {
233 @Override
234 public void run() {
235 try {
236 // 判断SD卡是否存在,并且是否具有读写权限
237 if (Environment.getExternalStorageState().equals(
238 Environment.MEDIA_MOUNTED)) {
239 // 获得存储卡的路径
240 String sdpath = Environment.getExternalStorageDirectory()
241 + "/";
242 mSavePath = sdpath + "download";
243 URL url = new URL(mHashMap.get("url"));
244 // 创建连接
245 HttpURLConnection conn = (HttpURLConnection) url
246 .openConnection();
247 conn.connect();
248 // 获取文件大小
249 int length = conn.getContentLength();
250 // 创建输入流
251 InputStream is = conn.getInputStream();
252
253 File file = new File(mSavePath);
254 // 判断文件目录是否存在
255 if (!file.exists()) {
256 file.mkdir();
257 }
258 File apkFile = new File(mSavePath, mHashMap.get("name"));
259 FileOutputStream fos = new FileOutputStream(apkFile);
260 int count = 0;
261 // 缓存
262 byte buf[] = new byte[1024];
263 // 写入到文件中
264 do {
265 int numread = is.read(buf);
266 count += numread;
267 // 计算进度条位置
268 progress = (int) (((float) count / length) * 100);
269 // 更新进度
270 mHandler.sendEmptyMessage(DOWNLOAD);
271 if (numread <= 0) {
272 // 下载完成
273 mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
274 break;
275 }
276 // 写入文件
277 fos.write(buf, 0, numread);
278 } while (!cancelUpdate);// 点击取消就停止下载.
279 fos.close();
280 is.close();
281 }
282 } catch (MalformedURLException e) {
283 e.printStackTrace();
284 } catch (IOException e) {
285 e.printStackTrace();
286 }
287 // 取消下载对话框显示
288 mDownloadDialog.dismiss();
289 }
290 };
291
292 /**
293 * 安装APK文件
294 */
295 private void installApk() {
296 File apkfile = new File(mSavePath, mHashMap.get("name"));
297 if (!apkfile.exists()) {
298 return;
299 }
300 // 通过Intent安装APK文件
301 Intent i = new Intent(Intent.ACTION_VIEW);
302 i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
303 "application/vnd.android.package-archive");
304 mContext.startActivity(i);
305 }
306 }
ParseXmlService.java

1 public class ParseXmlService

2 {
3 public HashMap<String, String> parseXml(InputStream inStream) throws Exception
4 {
5 HashMap<String, String> hashMap = new HashMap<String, String>();
6
7 // 实例化一个文档构建器工厂
8 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
9 // 通过文档构建器工厂获取一个文档构建器
10 DocumentBuilder builder = factory.newDocumentBuilder();
11 // 通过文档通过文档构建器构建一个文档实例
12 Document document = builder.parse(inStream);
13 //获取XML文件根节点
14 Element root = document.getDocumentElement();
15 //获得所有子节点
16 NodeList childNodes = root.getChildNodes();
17 for (int j = 0; j < childNodes.getLength(); j++)
18 {
19 //遍历子节点
20 Node childNode = (Node) childNodes.item(j);
21 if (childNode.getNodeType() == Node.ELEMENT_NODE)
22 {
23 Element childElement = (Element) childNode;
24 //版本号
25 if ("version".equals(childElement.getNodeName()))
26 {
27 hashMap.put("version",childElement.getFirstChild().getNodeValue());
28 }
29 //软件名称
30 else if (("name".equals(childElement.getNodeName())))
31 {
32 hashMap.put("name",childElement.getFirstChild().getNodeValue());
33 }
34 //下载地址
35 else if (("url".equals(childElement.getNodeName())))
36 {
37 hashMap.put("url",childElement.getFirstChild().getNodeValue());
38 }
39 }
40 }
41 return hashMap;
42 }
43
44 /**
45 * 获取软件版本号
46 *
47 * @param context
48 * @return
49 */
50 private int getVersionCode(Context context)
51 {
52 int versionCode = 0;
53 try
54 {
55 // 获取软件版本号,
56 versionCode = context.getPackageManager().getPackageInfo("com.szy.update", 0).versionCode;
57 } catch (NameNotFoundException e)
58 {
59 e.printStackTrace();
60 }
61 return versionCode;
62 }
63 }
服务器上面的版本配置文件:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: