安卓技术版本更新
来源:互联网 发布:音乐唱歌软件 编辑:程序博客网 时间:2024/05/04 07:43
MainActivity.java:
public class MainActivity extends Activity {
private AlertDialog dialog;
private Button btnUpdate;
private ListView lv;
private String path1 = "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=0&pageSize=20";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找控件
btnUpdate = (Button) findViewById(R.id.fin);
// 设置版本更新的监听
btnUpdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("软件版本更新").setPositiveButton("立即更新", onclick)
.setNegativeButton("以后再说", null).create();
//开启线程检查版本信息
new Thread(new CheckVersionTask()).start();
}
});
}
/**
* 版本更新的Hnadler
*/
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
dialog.setMessage(updateEntity.getUpdateLog());
dialog.show();
break;
case 1:
downLoadApk();
break;
}
};
};
/*
* 从服务器中下载APK
*/
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.getDownloadUrl(), pd);
sleep(3000);
UpdateTools tools = new UpdateTools();
//安装APk
tools.installApk(file,MainActivity.this);
pd.dismiss(); //结束掉进度条对话框
} catch (Exception e) {
e.printStackTrace();
}
}}.start();
}
/**
* 下载方法
*
* @param path
* @param pd
* @return
* @throws Exception
*/
public File getFileFromServer(String path, ProgressDialog pd)
throws Exception {
// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
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;
}
}
/*
* 从服务器获取xml解析并进行比对版本号
*/
private Bean updateEntity;
public class CheckVersionTask implements Runnable{
public void run() {
try {
//从资源文件获取服务器 地址
String path = "http://www.oschina.net/MobileAppVersion.xml";
//包装成url的对象
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.getVersionCode()) <= versionCode){
Log.i("xxx","版本号相同无需升级");
}else{
Log.i("xxxx","版本号不同 ,提示用户升级 ");
handler.sendEmptyMessage(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 立即更新的监听
*/
DialogInterface.OnClickListener onclick = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.sendEmptyMessage(1);
}
};
}
Bean.java:
public class Bean {
private String versionCode;
private String versionName;
private String downloadUrl;
private String updateLog;
public String getUpdateLog() {
return updateLog;
}
public void setUpdateLog(String updateLog) {
this.updateLog = updateLog;
}
public String getVersionCode() {
return versionCode;
}
public void setVersionCode(String versionCode) {
this.versionCode = versionCode;
}
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public String getDownloadUrl() {
return downloadUrl;
}
public void setDownloadUrl(String downloadUrl) {
this.downloadUrl = downloadUrl;
}
@Override
public String toString() {
return "Bean [versionCode=" + versionCode + ", versionName="
+ versionName + ", downloadUrl=" + downloadUrl + "]";
}
}
UpdateTools.java
public class UpdateTools {
/*
* 用pull解析器解析服务器返回的xml文件 (xml封装了版本号)
*/
public static Bean getUpdataInfo(InputStream is) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");// 设置解析的数据源
int type = parser.getEventType();
Bean info = new Bean();// 实体
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if ("versionCode".equals(parser.getName())) {
info.setVersionCode(parser.nextText()); // 获取版本号
} else if ("downloadUrl".equals(parser.getName())) {
info.setDownloadUrl(parser.nextText()); // 获取要升级的APK文件
} else if ("updateLog".equals(parser.getName())) {
info.setUpdateLog(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);
}
}
- 安卓技术版本更新
- 安卓版本更新
- 安卓版本更新
- 安卓版本更新
- 安卓 版本更新
- 安卓应用的版本更新
- 安卓版本更新的简单Demo
- 安卓版本更新的简单Demo
- 安卓版本更新的逻辑
- 安卓app版本更新安装失败
- 安卓版本更新简单的Demo
- 解决安卓SDK无法更新其他版本问题。
- 安卓APP采用观察者模式实现检测版本更新
- 安卓APP采用观察者模式实现检测版本更新
- 安卓项目版本更新(保存xml数据)
- Eclipse实现安卓客户端的版本更新
- 安卓版本更新后进入引导界面
- 安卓APP版本更新及自动打开
- swig简单应用实战
- 获取设备及浏览器等的高度和宽度
- The script to install openvpn (by quqi99)
- 理解Node模块(module)
- 前端学习CSS篇(二)
- 安卓技术版本更新
- springmvc中的乱码问题解决方案
- Android中Snackbar的使用
- 计算机图形学基础-三维变换
- 二维数组作为参数需要注意的问题
- POJ 1465-Multiple(BFS-最小整倍数)
- hdu 1671
- 浅谈spring mvc和struts的比较
- 关于http