Android笔记之版本更新
来源:互联网 发布:日本买什么划算 知乎 编辑:程序博客网 时间:2024/06/11 03:38
安卓版本更新
1.在应用首界面onCreate中调用initVersion();
/**
* 更新版本
*/
private void initVersion() {
// TODO Auto-generated method stub
// 获取版本号
getLocalVersion();
String url = Urls.VERSION_CODE;
Log.i("更新版本url---", url);
Request<String> request = NoHttp.createStringRequest(url,
RequestMethod.POST);
queue.add(222, request, new OnResponseListener<String>() {
@Override
public void onStart(int what) {
// TODO Auto-generated method stub
}
@Override
public void onSucceed(int what, Response<String> response) {
// TODO Auto-generated method stub
Log.i("onSucceed---", response.get());
Gson gson = new Gson();
VersionUpdate update = gson.fromJson(response.get(),
VersionUpdate.class);
String apkUrl = update.appmanage.appUrl;
// 最新apk下载地址
String newversion_code = update.appmanage.version_code;
int new_version_code = Integer.parseInt(newversion_code);
String errorCodes = update.errorCode;
Log.i("appUrl---", apkUrl);
Log.i("version_code---", newversion_code);
Log.i("errorCode2---", errorCodes);
if ("0".equals(errorCodes)) {
// 0 没有获取到后台版本号
} else if ("1".equals(errorCodes)) {
// 1 获取到后台版本号
if (new_version_code == localCode) {
// 相同 说明当前已经是最新版本
return;
} else {
// 说明有新版本需要更新
// 弹对话框提示用户去下载新版本
showUpdateDialog(apkUrl);
}
}
}
@Override
public void onFailed(int what, String url, Object tag,
Exception exception, int responseCode, long networkMillis) {
// TODO Auto-generated method stub
}
@Override
public void onFinish(int what) {
// TODO Auto-generated method stub
}
});
}
2.获取应用当前版本号
private int localCode;
/**
* 获取应用当前版本号
*/
private void getLocalVersion() {
// 如何拿到版本信息?
pm = context.getPackageManager();
// 获取到对应的包下面的信息
// 写0获取到所有的 其他的代表写谁获取谁
try {
// PackageInfo 是对整个清单文件的封装
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
// ApplicationInfo 是Application节点的封装
localCode = packageInfo.versionCode;
//这里只获取localCode进行判断
} catch (PackageManager.NameNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3.下载新版本
private AlertDialog alertDialog;
/**
* 下载新版本
*
* @param info
* @param apkUrl
*/
private void showUpdateDialog(final String apkUrl) {
// 普通对对话框
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
alertDialog = builder.create();
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
final View layout = inflater.inflate(R.layout.dialogversion, null);
// TextView tv_info = (TextView)
// layout.findViewById(R.id.tv_dialoge_info);
Button bt_cancle = (Button) layout.findViewById(R.id.bt_cancle_dialog);
Button bt_sure = (Button) layout.findViewById(R.id.bt_sure_dialog);
// tv_info.setText(info);
alertDialog.setView(layout);
alertDialog.setIcon(R.drawable.zc);
// 设置ProgressDialog 是否可以按退回按键取消
alertDialog.setCancelable(false);
// 如果不想让对话框关闭
// builder.setCancelable(false);
// 给对话框设置一个消失的监听
alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
}
});
bt_cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO 点击的是(暂不更新)
alertDialog.dismiss();
}
});
bt_sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO 点击的是(立即更新)
alertDialog.dismiss();
// 去下载
// 自己 NoHttp // 从哪下 下到哪
downloadApk(apkUrl);
}
});
alertDialog.show();
}
3.1 dialogversion布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@drawable/shape_dialoge"
android:orientation="vertical">
<TextView
android:id="@+id/tv_dialoge_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="检测到新版本"
android:textColor="@color/black"
android:gravity="center"
android:textSize="20dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#3EB3F9" />
<!-- <TextView
android:id="@+id/tv_content_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="更新内容:"
android:textColor="@color/black"
android:textSize="18dp" />
<TextView
android:id="@+id/tv_dialoge_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="XXXXXXXXXXXXXX"
android:textColor="@color/black"
android:textSize="16dp" /> -->
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#3EB3F9" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt_cancle_dialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="100"
android:background="#fff"
android:text="暂不更新"
android:textColor="#919191"
android:textSize="20dp" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#3EB3F9" />
<Button
android:id="@+id/bt_sure_dialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="100"
android:background="#fff"
android:text="立即更新"
android:textColor="#3EB3F9"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
3.2 zc样式
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android";>
<item>
<shape>
<solid android:color="#dddddd" />
</shape>
</item>
<item
android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp">
<shape>
<solid android:color="#f4f4f4"/>
</shape>
</item>
</layer-list>
4.下载appk过程
private AlertDialog pd;
private void downloadApk(String apkurl) {
// 下载维护的线程池数
DownloadQueue downloadQueue = NoHttp.newDownloadQueue();
Log.i("sdcard--", MyResource.DOWNLOAD_PATH);
// 下载请求
DownloadRequest downloadRequest = NoHttp.createDownloadRequest(apkurl,
RequestMethod.GET, MyResource.DOWNLOAD_PATH,
"example.apk", true, true);
// 下载请求添加在下载队列里
downloadQueue.add(3, downloadRequest, new DownloadListener() {
private ProgressBar pb_progress;
private TextView tv_max;
private TextView tv_progress;
public long allCount;
@Override
public void onDownloadError(int what, Exception exception) {
Toast.makeText(context, "服务器忙,下载失败", Toast.LENGTH_SHORT).show();
Toast.makeText(context, exception.getMessage(),
Toast.LENGTH_SHORT).show();
// 对话框消失掉
if (pd != null) {
pd.dismiss();
}
}
@Override
public void onStart(int what, boolean isResume, long rangeSize,
Headers responseHeaders, long allCount) {
this.allCount = allCount;
final AlertDialog.Builder builder = new AlertDialog.Builder(
context);
pd = builder.create();
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
final View layout = inflater.inflate(R.layout.dialog_download,
null);
tv_progress = (TextView) layout.findViewById(R.id.tv_progress);
tv_max = (TextView) layout.findViewById(R.id.tv_max);
pb_progress = (ProgressBar) layout
.findViewById(R.id.pb_progress);
pd.setView(layout);
DecimalFormat df = new DecimalFormat("##.##");
tv_max.setText(df.format((allCount / 1024.00 / 1024.00)) + "M");
pd.setCancelable(false);
pb_progress.setMax(100);
pd.show();
}
@Override
public void onProgress(int what, int progress, long fileCount) {
//正在下载
// 更新进度条
pb_progress.setProgress(progress);
DecimalFormat df = new DecimalFormat("##.##");
tv_progress.setText(df.format((double) progress
* (allCount / 1024.00 / 1024.00 / 100.00))
+ "M");
}
@Override
public void onFinish(int what, String filePath) {
// 下载完成
// 对话框消失掉
if (pd != null) {
pd.dismiss();
}
ToastUtils.toast(context, "下载成功");
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setDataAndType(
Uri.parse("file://" + MyResource.DOWNLOAD_PATH
+ "/example.apk"),
"application/vnd.android.package-archive");
// 开启B同时还要索要结果
startActivityForResult(intent, REQUEST_FOR_INSTALL);
}
@Override
public void onCancel(int what) {
// TODO Auto-generated method stub
// 对话框消失掉
if (pd != null) {
pd.dismiss();
}
}
});
}
4.1 MyResource工具类
/**
* 用来存放一些公用的资源 比如 下载的路径
* @author Administrator
*
*/
public class MyResource {
// 我们将跟我们软件相关的所有的文件放在sd卡的同一个路径下
//apk名字
private static final String ROOT = "example";
// /mnt/sdcard/example
public static final String ROOT_PATH = Environment.getExternalStorageDirectory().getPath()+"/"+ROOT;
// 这个目录只跟下载的东西相关
// /mnt/sdcard/example/download
public static final String DOWNLOAD_PATH = ROOT_PATH+"/download";
}
4.2 dialog_download布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">
<TextView
android:id="@+id/tv_dialoge_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="下载中。。。"
android:gravity="center"
android:textSize="20dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="@dimen/x1"
android:background="#20abff" />
<ProgressBar
android:id="@+id/pb_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:progressDrawable="@drawable/progressbar_background"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
/>
<LinearLayout
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<LinearLayout
android:layout_marginRight="40dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3M"
android:textSize="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" / "
android:textSize="16dp" />
<TextView
android:id="@+id/tv_max"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5M"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
4.3 @drawable/progressbar_background 布局
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"; >
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dp" />
<solid android:color="#A5D1DB" /> //这个颜色是progressbar的背景颜色
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dp" />
<solid android:color="#1E90FF" /> //这个颜色是progressbar的进度颜色
</shape>
</clip>
</item>
</layer-list>
4.4 在这获取B界面的结果回传。
protected static final int REQUEST_FOR_INSTALL = 1;
// 在这获取B界面的结果回传
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_FOR_INSTALL) {
// 说明是我们的界面回传
// 如果以后再开启系统的一个界面 这个界面包含取消和确定 我想知道点的是取消还是确定 做法都是通用的
if (resultCode == REQUEST_FOR_INSTALL) {
// 说明用户点击确定
System.out.println("点击ok");
} else {
// 说明用户点击取消
Log.i("onActivityResult", "安装取消");
//TODO 跳转至首页
Intent intent = new Intent();
intent.setClass(context, MainAct.class);
startActivity(intent);
}
}
}
阅读全文
0 0
- Android笔记之版本更新
- Android之版本更新
- Android 之版本更新
- Android之Apk版本更新
- Android功能模块化之版本更新
- Android之版本检测和更新
- android 版本更新之JAR的应用
- Android开发之版本检查更新
- Android 版本更新之增量更新 包含java、.net服务端
- Android 版本更新之增量更新 包含java、.net服务端
- android:versionCode、android:versionName之实现第一次登陆、版本更新
- Android版本更新
- Android SDK版本更新
- Android软件版本更新
- Android 检测版本更新
- Android版本更新实现
- android版本更新问题
- android apk版本更新
- 1682亿!!阿里工程师如何喝着茶创造双11奇迹?
- 有相同键值的json合并为一个数组
- 【算法题】求数列中所有数字的小和之和问题
- C++设计模式实例讲解
- 使用PreparedStatement的setString方法会自动在数据库相应表项后面补空格解决办法
- Android笔记之版本更新
- c++ primer 笔记,第十章(泛型算法)
- 移动前端
- Effective Java 3 -- Singleton的优化实现
- 超全的深度剖析内存系列——3.虚拟内存(二)
- Java中JDK,JRE,JVM的区别
- Static用法
- OpenCV_用类处理彩色图像
- #项目导入AS出错:Error:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details