实现Splash页面版本自动更新
来源:互联网 发布:淘宝泳装买家秀 编辑:程序博客网 时间:2024/05/22 09:49
获取应用程序版本号的代码:
//用PackageManager拿到PackageInfo,PackageInfo中的versionName
PackageInfo packinfo = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
String version = packinfo.versionName;
获取服务器版本号的代码:
//获取服务器地址
String path = getResources().getString(R.string.url);
URL url = new URL(path);
//创建网络连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
//发出请求,获得返回码
int code = conn.getResponseCode();
if(code ==200){
//获取服务器返回的流并进行解析
InputStream is = conn.getInputStream();
String result = StreamTools.readStream(is);
//转化为json并解析出版本号
JSONObject json = new JSONObject(result);
String serverVersion = json.getString("version");
Log.i(TAG,"服务器版本:"+serverVersion);
}
将流转化为字符串的代码:
public static String readStream(InputStream is) throws IOException{
//ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型数组的缓冲区,缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray()和 toString()获取数据
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
is.close();
return baos.toString();
}
弹出对话框的代码:
AlertDialog.Builder builder =new Builder(this);
builder.setCancelable(false);
builder.setTitle("升级提醒");
builder.setMessage(desc);
builder.setPositiveButton("立即升级",new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog,int which) {
pd = new ProgressDialog(SplashActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
HttpUtils http = new HttpUtils();
File sdDir = Environment.getExternalStorageDirectory();
File file = new File(sdDir, SystemClock.uptimeMillis()+".apk");
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
http.download(downloadpath, file.getAbsolutePath(),new RequestCallBack<File>(){
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(SplashActivity.this,"下载失败", 0).show();
pd.dismiss();
loadMainUI();
}
public void onLoading(long total,long current, boolean isUploading) {
pd.setMax((int) total);
pd.setProgress((int) current);
};
public void onSuccess(ResponseInfo<File> fileinfo) {
pd.dismiss();
Toast.makeText(SplashActivity.this,"下载成功", 0).show();
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri
.fromFile(fileinfo.result),
"application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}else {
Toast.makeText(SplashActivity.this,"SDcard不可用,不可以自动更新", 0).show();
loadMainUI();
}
}
});
builder.setNegativeButton("稍后再说",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
loadMainUI();
}
});
builder.show();
}
下载apk的操作:
1.使用开源框架xUtils
2.使用HttpUtils的download方法,填入三个参数:服务器下载地址,手机中的存储位置、回调事件
3.回调事件中有三个常用的方法:onSuccess下载成功、onFailure下载失败、onLoading更新下载进度
安装apk的代码:
1.调用系统的安装apk的界面,传入对应的参数
2.具体实现方式
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(
Uri.fromFile(fileinfo.result),
"application/vnd.android.package-archive");
startActivity(intent);
- 实现Splash页面版本自动更新
- zaker splash页面实现
- C# 实现版本自动更新
- 实现android应用版本自动更新
- Android实现App版本自动更新
- Android实现App版本自动更新
- Android实现App版本自动更新
- Android实现App版本自动更新
- iOS中Splash欢迎页面的实现
- android首页Splash页面简单实现
- Splash 页面
- Android实现APP版本自动更新功能
- 自动更新版本
- 页面自动更新
- 手机安全卫士------Splash页面
- Android Splash 页面
- Splash动态页面爬虫
- (转)版本自动更新程序及3种实现策略
- Facebook给员工高效工作的26个建议
- Sublime Text 3 快捷键汇总
- 开通博客了!!!!!
- 怎么查找XCode模拟器的保存路径?以及真机的沙盒文件?
- 6410平台500W摄像头调试过程 && 拍照偏绿和图片保存等BUG
- 实现Splash页面版本自动更新
- Hadoop系列-YARN RM HA 高可用集群
- 剑指offer—关于判断二叉树是否为平衡二叉树
- PAT (Basic Level) Practise (中文)1014.福尔摩斯的约会
- Redis实现类似SQL的where多条件查询
- Css背景图片知识总结
- 时间系统、进程的调度与切换
- AVL树的基本实现
- unix线程控制