使用公共类
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.meikuang.landongjiguang.renyuandingwei.R;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
public class UpdateManager {
private static final int DOWNLOAD = 1;
private static final int DOWNLOAD_FINISH = 2;
HashMap<String, String> mHashMap;
private String mSavePath;
private int progress;
private boolean cancelUpdate = false;
private Context mContext;
private ProgressBar mProgress;
private Dialog mDownloadDialog;
private String string;//网址
private Handler mHandler = new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what)
{
case DOWNLOAD:
mProgress.setProgress(progress);
break;
case DOWNLOAD_FINISH:
installApk();
break;
default:
break;
}
}
};
public UpdateManager(Context context,String s)
{
this.mContext = context;
this.string = s;
}
public void checkUpdate()throws Resources.NotFoundException, IOException {
new Thread(new Runnable() {
@Override
public void run() {
try {
if (isUpdate()) {
// 显示提示对话框
Looper.prepare();
showNoticeDialog();
Looper.loop();
// Log.d("消息", "有新版本");
} else {
// Log.d("消息", "已是最新版本");
Handler.sendEmptyMessage(0);
}
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
}
}).start();
}
// {
// if (isUpdate())
// {
// showNoticeDialog();
// } else
// {
// Toast.makeText(mContext, "此为最新版本,无需升级", Toast.LENGTH_LONG).show();
// }
// }
/**
* 土司提示
* */
private Handler Handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
toastMessage();
break;
default:
break;
}
}
};
protected void toastMessage(){
Toast.makeText(mContext, "此为最新版本,无需升级", Toast.LENGTH_LONG).show();
}
private boolean isUpdate() {
URL url=null;
try {
url = new URL(string +"//mhy//version.xml");//网络配置文件
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
int versionCode = getVersionCode(mContext);
HttpURLConnection conn = null;
InputStream inStream=null;
try {
conn = (HttpURLConnection) url.openConnection();
} catch (IOException e2) {
e2.printStackTrace();
}//基于HTTP协议连接对象
conn.setConnectTimeout(5000);
try {
conn.setRequestMethod("GET");
} catch (ProtocolException e1) {
e1.printStackTrace();
}
try {
if(conn.getResponseCode() == 200){
inStream = conn.getInputStream();
}
} catch (IOException e1) {
e1.printStackTrace();
}
ParseXmlService service = new ParseXmlService();
try {
mHashMap = service.parseXml(inStream);
} catch (Exception e)
{
e.printStackTrace();
}
if (null != mHashMap) {
int serviceCode = Integer.valueOf(mHashMap.get("version"));
if (serviceCode > versionCode) {
return true;
}
}
return false;
}
private int getVersionCode(Context context)
{
int versionCode = 0;
try {
versionCode = context.getPackageManager().getPackageInfo("包名", 0).versionCode;//需要输入app包名
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
private void showNoticeDialog()
{
Builder builder = new Builder(mContext);
builder.setTitle("发现新版本");
builder.setMessage("是否需要更新?");
builder.setPositiveButton("是", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
showDownloadDialog();
}
});
builder.setNegativeButton("否", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
Dialog noticeDialog = builder.create();
noticeDialog.show();
}
private void showDownloadDialog()
{
Builder builder = new Builder(mContext);
builder.setTitle("正在更新新版本");
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.softupdate_progress, null);
mProgress = (ProgressBar) v.findViewById(R.id.update_progress);
builder.setView(v);
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
cancelUpdate = true;
}
});
mDownloadDialog = builder.create();
mDownloadDialog.show();
downloadApk();
}
private void downloadApk() {
new downloadApkThread().start();
}
private class downloadApkThread extends Thread {
@Override
public void run() {
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String sdpath = Environment.getExternalStorageDirectory() + "/";
mSavePath = sdpath + "download";
URL url = new URL(mHashMap.get("url"));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
File file = new File(mSavePath);
if (!file.exists()) {
file.mkdir();
}
File apkFile = new File(mSavePath, mHashMap.get("name"));
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(DOWNLOAD);
if (numread <= 0) {
mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
break;
}
fos.write(buf, 0, numread);
} while (!cancelUpdate);
fos.close();
is.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mDownloadDialog.dismiss();
}
};
private void installApk() {
File apkfile = new File(mSavePath, mHashMap.get("name"));
if (!apkfile.exists())
{
return;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/包名");//本地存放apk(下载后安装用)
mContext.startActivity(i);
}
}
升级时对话框xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/update_progress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal" />
</LinearLayout>
用法
Toast.makeText(getActivity(), "开始检查更新", Toast.LENGTH_LONG).show();
UpdateManager manager = new UpdateManager(context(),服务器地址);//配置下载
try {
manager.checkUpdate();
} catch (IOException e) {
e.printStackTrace();
}