标签:
1.在后台维护一个Android的版本号,当每次进入APP的时候,在欢迎界面时,都去查询这个最新的版本号.和当前APP的版本对比.
2.将最新的APP(最新版本号)放在服务器上,并且提供一个下载功能的url(可以在后台维护一个上传最新的APP的一个功能).
/**
* 下载app
*
* @param url
* @return
*/
public static Object downloadPdaLookUp(String action, final Context mContext) {
String result = "[{\"note\":\"网络异常!请检查网络!\",\"networkflag\":true}]";
if (!NetworkUtil.getNetworkStatus()) {
return result;
}
String realurl = s_url_lookup + action;
LogUtil.i(TAG, "" + realurl);
try {
HttpClient httpClient = new HttpClient();
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
ActivityUtil.userName_c, ActivityUtil.password_c);
LogUtil.i(TAG + "用户", ActivityUtil.userName_c
+ ActivityUtil.password_c);
httpClient.getState().setCredentials(AuthScope.ANY, creds);
// 网络连接时间10秒
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 20 * 1000);
// server端返回数据时间10秒
httpClient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 50 * 1000);
PostMethod method = new PostMethod(realurl);
method.setDoAuthentication(true);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
int statusCode = httpClient.executeMethod(method);
if (statusCode == 200) {
InputStream is;
is = method.getResponseBodyAsStream();
String SDCard = Environment.getExternalStorageDirectory() + "";
String pathName = "/sdcard" + "/pda.apk";// 文件存储路径
// /storage/emulated/0/pda.apk
File file = new File(pathName);
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int count = 0;
while ((count = is.read(buf)) != -1) {
fos.write(buf, 0, count);
}
fos.close();
is.close();
openFile(file, mContext);
result = "[{\"note\":\"下载完成!\",\"status\":true}]";
return result;
}
} catch (ClientProtocolException e) {
// e.printStackTrace();
result = "[{\"note\":\"网络协议异常(ClientProtocolException)!\",\"networkflag\":true}]";
return result;
} catch (IOException e) {
// e.printStackTrace();
result = "[{\"note\":\"后台或网络(IOException)异常!\",\"networkflag\":true}]";
return result;
}
result = "[{\"note\":\"未知错误!请联系开发人员!\",\"networkflag\":true}]";
return result;
}
/**
* 下载完pda后,自动打开安装程序.
*
* @param file
* @param mContext
*/
private static void openFile(File file, final Context mContext) {
// TODO Auto-generated method stub
LogUtil.i("OpenFile", file.getName());
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
mContext.startActivity(intent);
}
标签:
原文地址:http://blog.csdn.net/want_water_fish/article/details/51363327