## MainActivity中的内容 ##
/**
* 消息机制
*/
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
dialog.setMessage(updateEntity.getDescription());
dialog.show();
break;
case 1:
downLoadApk();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new AlertDialog.Builder(MainActivity.this).
setTitle("升级提醒").
setIcon(R.drawable.ic_launcher).
setPositiveButton("在线升级", onclick).
setNegativeButton("不想升级", null).
create();
new Thread(new CheckVersionTask()).start();
}
DialogInterface.OnClickListener onclick = new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
handler.sendEmptyMessage(1);
}
};
protected void downLoadApk() {
final ProgressDialog pd;
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载更新");
pd.show();
new Thread(){
@Override
public void run() {
try {
File file = getFileFromServer(updateEntity.getUrl(), pd);
sleep(3000);
UpdateTools tools = new UpdateTools();
tools.installApk(file,MainActivity.this);
pd.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}}.start();
}
public class CheckVersionTask implements Runnable{
public void run() {
try {
String path = getResources().getString(R.string.serverurl);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
InputStream is =conn.getInputStream();
updateEntity = UpdateTools.getUpdataInfo(is);
int versionCode = MainActivity.this.getPackageManager().getPackageInfo(MainActivity.this.getPackageName(), 0).versionCode;
if(Integer.parseInt(updateEntity.getVersion()) <= versionCode){
Log.i("xxx","版本号相同无需升级");
}else{
Log.i("xxxx","版本号不同 ,提示用户升级 ");
handler.sendEmptyMessage(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 下载方法
*
* @param path
* @param pd
* @return
* @throws Exception
*/
public File getFileFromServer(String path, ProgressDialog pd)
throws Exception {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
pd.setMax(conn.getContentLength());
InputStream is = conn.getInputStream();
File file = new File(Environment.getExternalStorageDirectory(),
"updata.apk");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
int total = 0;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
total += len;
pd.setProgress(total);
}
fos.close();
bis.close();
is.close();
return file;
} else {
return null;
}
}
@Override
protected void onPause() {
Log.w(Conf.TAG, "Activity1.onPause()");
super.onPause();
}
@Override
protected void onResume() {
Log.w(Conf.TAG, "Activity1.onResume()");
super.onResume();
}
## 工具类 ##
public static UpdateEntity getUpdataInfo(InputStream is) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");
int type = parser.getEventType();
UpdateEntity info = new UpdateEntity();
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if ("version".equals(parser.getName())) {
info.setVersion(parser.nextText());
} else if ("url".equals(parser.getName())) {
info.setUrl(parser.nextText());
} else if ("description".equals(parser.getName())) {
info.setDescription(parser.nextText());
}
break;
}
type = parser.next();
}
return info;
}
/**
*
* 安装Apk
*
* @param file
* @param context
*/
public void installApk(File file,Context context) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
}
## Bean包 ##
public class UpdateEntity {
private String version;
private String url;
private String description;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}