均属于笔记,仅供个人参考,有问题欢迎指正
只看主要的过程方法,对于MD5和完整性的校验不再处理
/**
* 自动更新工具类
*/
public class AutoUpdate {
public Activity activity = null;
private String strURL = "http://168.0.0.1:81/Demos.apk";
private ProgressDialog dialog;
public AutoUpdate(Activity activity, String strURL) {
this.activity = activity;
this.strURL = strURL;
}
//入口
public void update() {
if (isNetworkAvailable(this.activity) == false) {
return;
}
String fileEx = strURL.substring(strURL.lastIndexOf(".") + 1,
strURL.length()).toLowerCase();
if (!fileEx.equals("apk"))
Toast.makeText(activity, "下载的文件不是APP安装包!", Toast.LENGTH_SHORT)
.show();
else {
showUpdateDialog();
}
}
public static boolean isNetworkAvailable(Context ctx) {
try {
ConnectivityManager cm = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
return (info != null && info.isConnected());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void showUpdateDialog() {
run(strURL);
showWaitDialog();
}
public void showWaitDialog() {
dialog = new ProgressDialog(activity);
dialog.setMessage("正在进行更新");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
private void run(final String strPath) {
try {
Runnable r = new Runnable() {
@Override
public void run() {
try {
doDownloadTheFile(strPath);
} catch (StringIndexOutOfBoundsException e) {
e.printStackTrace();
handler.sendMessage(handler.obtainMessage(0));
} catch (Exception e) {
e.printStackTrace();
handler.sendMessage(handler.obtainMessage(0));
}
}
};
new Thread(r).start();
} catch (Exception e) {
e.printStackTrace();
handler.sendMessage(handler.obtainMessage(0));
}
}
@SuppressWarnings("resource")
private void doDownloadTheFile(String strPath) throws Exception {
URL myURL = new URL(strPath);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
File down = new File(Constant.FILEPATH);
if (!down.exists())
down.mkdirs();
String currentTempFilePath = Constant.FILEPATH
+ getNowDateTime() + ".apk";
File myTempFile = new File(currentTempFilePath);
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);
dialog.cancel();
dialog.dismiss();
openFile(myTempFile);
is.close();
}
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);
}
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;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Toast.makeText(activity, "安装包下载失败!", Toast.LENGTH_LONG).show();
dialog.cancel();
}
};
private String getNowDateTime() {
SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMddHHmmss",
Locale.CHINA);
Date date1 = new Date();
return sf1.format(date1);
}
}