Android应用自动更新功能的实现!

Android应用自动更新功能的实现!
http://blog.csdn.net/android_tutor/article/details/7015986

private static final int DOWN_UPDATE = 1;
private static final int DOWN_OVER = 2;
private String apkUrl = "http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk"; private static String saveFileName = savePath + ""; private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case DOWN_UPDATE:
mProgress.setProgress(progress);
break;
case DOWN_OVER:
installAPK();
break;
default:
break;
}
}
}; LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.progress, null); private Runnable mdownApkRunnable = new Runnable() {
public void run() {
try {
URL url = new URL(apkUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
int length = conn.getContentLength(); InputStream is = conn.getInputStream(); /*File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}*/
String apkFile = saveFileName;
File ApkFile = new File(apkFile);
FileOutputStream fos = new FileOutputStream(ApkFile); int count = 0;
byte buf[] = new byte[1024]; do {
int numRead = is.read(buf);
count += numRead;
progress = (int) ( ((float) count / length) * 100 );
mHandler.sendEmptyMessage(DOWN_UPDATE);
if (numRead <= 0) {
mHandler.sendEmptyMessage(DOWN_OVER);
break;
}
fos.write(buf, 0, numRead);
} while (!interceptFlag); fos.close();
is.close(); } catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} private void installApk() {
File apkFile = new File(saveFileName);
if (!apkFile.exists()) {
return;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkFile.toString()), "application/vnd.android.package-archive");
mContext.startActivity(i);
}
上一篇:谈谈UIView的几个layout方法


下一篇:Android应用自动更新功能的实现!!!