由于大家提出的注释的问题,我做了一些修改,进行了一定的注释说明,更新一下,希望大家多多支持,谢谢!
package com.autoupdate;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;
import android.webkit.URLUtil;
import com.autoupdate.R;
/**
* 版本检测,自动更新
*
* @author shenyj-ydrh 1.通过Url检测更新 2.下载并安装更新 3.删除临时路径
*
*/
public class MyAutoUpdate {
// 调用更新的Activity
public Activity activity = null;
// 当前版本号
public int versionCode = 0;
// 当前版本名称
public String versionName = "";
// 控制台信息标识
private static final String TAG = "AutoUpdate";
// 文件当前路径
private String currentFilePath = "";
// 安装包文件临时路径
private String currentTempFilePath = "";
// 获得文件扩展名字符串
private String fileEx = "";
// 获得文件名字符串
private String fileNa = "";
// 服务器地址
private String strURL = "http://127.0.0.1:8080/ApiDemos.apk";
private ProgressDialog dialog;
/**
* 构造方法,获得当前版本信息
*
* @param activity
*/
public MyAutoUpdate(Activity activity) {
this.activity = activity;
// 获得当前版本
getCurrentVersion();
}
/**
* 检测更新
*/
public void check() {
// 检测网络
if (isNetworkAvailable(this.activity) == false) {
return;
}
// 如果网络可用,检测到新版本
if (true) {
// 弹出对话框,选择是否需要更新版本
showUpdateDialog();
}
}
/**
* 检测是否有可用网络
*
* @param context
* @return 网络连接状态
*/
public static boolean isNetworkAvailable(Context context) {
try {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// 获取网络信息
NetworkInfo info = cm.getActiveNetworkInfo();
// 返回检测的网络状态
return (info != null && info.isConnected());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 弹出对话框,选择是否需要更新版本
*/
public void showUpdateDialog() {
@SuppressWarnings("unused")
AlertDialog alert = new AlertDialog.Builder(this.activity)
.setTitle("新版本").setIcon(R.drawable.ic_launcher)
.setMessage("是否更新?")
.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 通过地址下载文件
downloadTheFile(strURL);
// 显示更新状态,进度条
showWaitDialog();
}
})
.setNegativeButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
/**
* 显示更新状态,进度条
*/
public void showWaitDialog() {
dialog = new ProgressDialog(activity);
dialog.setMessage("正在更新,请稍候...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
/**
* 获得当前版本信息
*/
public void getCurrentVersion() {
try {
// 获取应用包信息
PackageInfo info = activity.getPackageManager().getPackageInfo(
activity.getPackageName(), 0);
this.versionCode = info.versionCode;
this.versionName = info.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
/**
* 截取文件名称并执行下载
*
* @param strPath
*/
private void downloadTheFile(final String strPath) {
// 获得文件文件扩展名字符串
fileEx = strURL.substring(strURL.lastIndexOf(".") + 1, strURL.length())
.toLowerCase();
// 获得文件文件名字符串
fileNa = strURL.substring(strURL.lastIndexOf("/") + 1,
strURL.lastIndexOf("."));
try {
if (strPath.equals(currentFilePath)) {
doDownloadTheFile(strPath);
}
currentFilePath = strPath;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
// 执行下载
doDownloadTheFile(strPath);
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 执行新版本进行下载,并安装
*
* @param strPath
* @throws Exception
*/
private void doDownloadTheFile(String strPath) throws Exception {
Log.i(TAG, "getDataSource()");
// 判断strPath是否为网络地址
if (!URLUtil.isNetworkUrl(strPath)) {
Log.i(TAG, "服务器地址错误!");
} else {
URL myURL = new URL(strPath);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
if (is == null) {
throw new RuntimeException("stream is null");
}
//生成一个临时文件
File myTempFile = File.createTempFile(fileNa, "." + fileEx);
// 安装包文件临时路径
currentTempFilePath = myTempFile.getAbsolutePath();
FileOutputStream fos = new FileOutputStream(myTempFile);
byte buf[] = new byte[128];
do {
int numread = is.read(buf);
if (numread <= 0) {
break;
}
fos.write(buf, 0, numread);
} while (true);
Log.i(TAG, "getDataSource() Download ok...");
dialog.cancel();
dialog.dismiss();
// 打开文件
openFile(myTempFile);
try {
is.close();
} catch (Exception ex) {
Log.e(TAG, "getDataSource() error: " + ex.getMessage(), ex);
}
}
}
/**
* 打开文件进行安装
*
* @param f
*/
private void openFile(File f) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
// 获得下载好的文件类型
String type = getMIMEType(f);
// 打开各种类型文件
intent.setDataAndType(Uri.fromFile(f), type);
// 安装
activity.startActivity(intent);
}
/**
* 删除临时路径里的安装包
*/
public void delFile() {
Log.i(TAG, "The TempFile(" + currentTempFilePath + ") was deleted.");
File myFile = new File(currentTempFilePath);
if (myFile.exists()) {
myFile.delete();
}
}
/**
* 获得下载文件的类型
*
* @param f
* 文件名称
* @return 文件类型
*/
private String getMIMEType(File f) {
String type = "";
// 获得文件名称
String fName = f.getName();
// 获得文件扩展名
String end = fName
.substring(fName.lastIndexOf(".") + 1, fName.length())
.toLowerCase();
if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
|| end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
type = "audio";
} else if (end.equals("3gp") || end.equals("mp4")) {
type = "video";
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
|| end.equals("jpeg") || end.equals("bmp")) {
type = "image";
} else if (end.equals("apk")) {
type = "application/vnd.android.package-archive";
} else {
type = "*";
}
if (end.equals("apk")) {
} else {
type += "/*";
}
return type;
}
}
摘自:http://www.eoeandroid.com/thread-117764-1-12.html
分享到:
相关推荐
android是当今移动互联网最受欢迎的一门编程语言,互联网移动产品愈来愈多,版本更新成为一个必不可少的环节,本资源提供一个jar包来读取apk的配置文件来检测版本更新。
获取服务器端XML文件,解析XML获取到最新版本号,与当前软件版本号进行对比,发现不一样则提示更新。 涉及到内容点: 1.远程获取XML并分析XML内容 2.远程下载文件并打开 3.下载时通知栏显示下载进度
安卓Android源码——版本检测自动更新.zip
android 检查服务器更新版本,与服务器的版本号对比当前版本号,不同,就提示有新版本,点击下载就自动下载更新。
Android的版本进行检测与更新 1.检测更新 2.下载并进行更新安装 3.删除临时路径
Android项目版本检测自动更新.rar
android 消息推送功能、检测版本更新并下载更新版本安装功能的简易实现。
1,开启服务,请求获取版本号,判断是否需要更新 2,不需要更新,删除安装包 3,需要更新,则弹出对话框 4,点击更新按钮,从服务器下载apk到SD卡,同时在通知栏显示下载进度 5,下载完成之后,提示下载完成 6,开启...
android 版本检测更新、下载、及安装。
具体详见博客http://blog.csdn.net/u014727709/article/details/53466284
一个简单小巧的Android检查更新库,通过爬网页的方式查询应用在市场上的最新版本信息
进行检查更新时从服务器读取version.xml 进行版本检查, 更新时下载ftp上的apk文件,下载时通知栏进度条,下载完成后点击通知栏安装 准备工作 这里我默认环境已经安装完毕,只需要进行插件安装即可 Cordova/...
一般android app应用程序会隔一段时间发布一个新的版本,包括新增功能和修复原程序bug,当你打开某个app,如果检测到有最新的版本,会提醒你是否下载更新。
实现 android7、8 以上版本自动更新app 程序,仅供参考。
Android 版本检测和升级源码 完整版 发消息到状态栏下载有漂亮的进度条
android检测版本自动更新并弹出对话框提示用户下载,带有进度条,服务端解析xml,已测过好多次,主要包名更签名!
用于android app检测版本更新,使用简单的xml配置更新信息
做个网站的安卓客户端,用户安装到自己手机上,如果我出了新... 版本检测与更新有以下几个关键步骤: 1、检测有无新版本 2、下载新版本 3、安装替换新版本 我处理的方案是 1、在assets文件夹新增一个文件:ver.cfg,记
Android版本更新(检测、升级),检测版本,通过apk路径下载安装包,并进度条展示,安装