您的位置:首页 > 移动开发 > Android开发

Android 之应用程序自动更新

2016-07-28 18:11 405 查看
MainAcitivity类

自动更新应用程序的过程肯定要涉及到网络操作,所以要在主线程中执行网络操作,需要在主线程中添加如下代码

//使网络操作能在主线程中执行的代码

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()

.detectDiskReads()

.detectDiskWrites()

.detectNetwork()

.penaltyLog()

.build());

StricrMode.setVmPolicy(new StrictMode.VmPolicy.Builder()

.detectLeakedClosableObjects()

.penaltyLog()

.penaltyDeath()

.build());

//下面是调用检测安装应用程序的类

UpdateManager parseXmlService = new UpdateManager(MainActivity.this);

praseXmlService.checkUpdate();

UpdataInfo类

//这个类作为对象

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;

}

UpdateManager.java类

public void checkUpdate(){

if(isUpdate()){

showNoticeDialog();

}

else{

}

}

//检查版本是否有更新

public boolean isUpdate(){

int versionCode = getVersionCode(mContext);

URL url = null;

try{

url = new URL("http://baidu.con:8080/update/version.xml");

}catch(MalformedURLException e1){

e1.printStackTrace();

}

try{

InputStream is = ((HttpURLConnection)url.openConnection()).getInputStream();

HashMap<String,String> hashMap = new HashMap<String,String>();

try{

UpdataInfo info = this.getUpdataInfo(is);

hashMap.put("version",info.getVersion());

hashMap.put("url",info.getUrl());

hashMap.put("name",info.getDescription());

}catch(Exception e1){

e1.printStackTrace();

}

try{

mHashMap = hashMap;

}catch(Exception e){

e.printStackTrace();

}

}catch(Exceptoin e1){

e1.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("com.pack.ww",0).versionCode;

}catch(NameNotFoundException e){

e.printStackTrace();

}

return versionCode;

}

//提示对话框的函数

private void showNoticeDialog(){

AlertDialog.Builder builder = ne Builder(mContext);

builder.setTitle("版本升级");

builder.setMesssage("检测到最新版本请及时更新");

//下面是确定键的事件响应

builder.setPositiveButton("确定",new OnClickListener(){

@Override

public void onClick(DialogInterface dislog, 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 coid showDownloadDialog(){

AlertDialog.Builder builder = new Builder(mContext);

builder.setTitle("外卖服务台更新中");

//给下载对话框添加进度条

final LayoutInflater.inflate = LayoutInflater.form(mContext);

View view = inflater.inflate.(R.layout.upda,null);

mprogressBar = (ProgressBar)view.findViewById(R.id.progress);

builder

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: