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
}
自动更新应用程序的过程肯定要涉及到网络操作,所以要在主线程中执行网络操作,需要在主线程中添加如下代码
//使网络操作能在主线程中执行的代码
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
}
相关文章推荐
- Android 事件分发机制
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- android MediaRecorder录制音视频实现直播的基础
- 初识Android Studio目录
- 【Android进阶】实现各种各样的Tab切换效果
- ffmpeg支持x264的android平台编译
- android插件化学习笔记(二)
- Android Studio开发环境建立aidl文件,生成相应的java文件
- Android数据库两种操作方式介绍: SQLite与ORMLite
- ANDROID 监听返回键,重写事件
- Android系统的版本号与API的对应表
- Android 仿今日头条首页标题栏效果
- Android 获取设备网卡信息
- Android AndFix 热修复框架
- Android调用WEBSERVER发送信息到服务器
- Android 读取CPU信息方法
- Android ORMLite 框架的入门用法
- android获取内存信息方法
- Android 快速开发系列 ORMLite 框架最佳实践 标签: AndroidORMLite 2014-09-07 23:47 62916人阅读 评论(36) 收藏 举报 分类: 【Androi
- android中 IntDef注解的使用