需要两个权限:网络,SD卡读写权限
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends Activity {
String url = "http://www.oschina.net/MobileAppVersion.xml";
private XMLBean b;//实体类
private InputStream is;
private XMLBean data;
private int versionCode;
private String downloadUrl;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
data = (XMLBean) msg.obj;
versionCode = data.getVersionCode();
downloadUrl = data.getDownloadUrl();
int code = getVerCode(MainActivity.this);
if (code < versionCode) {
Toast.makeText(MainActivity.this, "版本过低", Toast.LENGTH_SHORT).show();
Log.d("tag", "地址" + downloadUrl);
getApk(downloadUrl);
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread() {
@Override
public void run() {
try {
getData();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
/**
* 获取软件版本号
*
* @param context
* @return
*/
public int getVerCode(Context context) {
int verCode = -1;
try {
//注意:"com.example.try_downloadfile_progress"对应AndroidManifest.xml里的package="……"部分
verCode = context.getPackageManager().getPackageInfo(
"com.bwie.shengji", 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
Log.e("msg", e.getMessage());
}
return verCode;
}
public void getData() throws Exception {
URL path = new URL(url);
HttpURLConnection hu = (HttpURLConnection) path.openConnection();
is = hu.getInputStream();
// 由android.util.Xml创建一个XmlPullParser实例
XmlPullParser xpp = Xml.newPullParser();
// 设置输入流 并指明编码方式
xpp.setInput(is, "UTF-8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
//开始标签
case XmlPullParser.START_TAG:
if (xpp.getName().equals("android")) {
b = new XMLBean();
} else if (xpp.getName().equals("versionCode")) {
eventType = xpp.next();
b.setVersionCode(Integer.parseInt(xpp.getText()));
} else if (xpp.getName().equals("downloadUrl")) {
eventType = xpp.next();
b.setDownloadUrl(xpp.getText());
}
break;
// 判断当前事件是否为标签元素结束事件
case XmlPullParser.END_TAG:
if (xpp.getName().equals("android")) {
Message msg = Message.obtain();
msg.obj = b;
handler.sendMessage(msg);
}
break;
}
eventType = xpp.next();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public void getApk(String path) {
HttpUtils hu = new HttpUtils();
File file = new File(Environment.getExternalStorageDirectory(), "kaiyuanzhongguo.apk");
//apk地址,存到本地的地址和名字,默认true,默认true,接口
hu.download(path, file.getPath(), true, true, new RequestCallBack<File>() {
@Override
public void onStart() {
super.onStart();
}
//下载成功
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
//使用隐式跳转安装apk
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "kaiyuanzhongguo.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
}
//下载中
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
}
@Override
public void onFailure(HttpException e, String s) {
}
});
}
}
版本升级需要两个系统权限:网络,sd卡的读写权限