修改版本升级,

This commit is contained in:
wds 2021-08-09 14:29:17 +08:00
parent 834fce7065
commit 4de8757925
7 changed files with 391 additions and 15 deletions

View File

@ -14,6 +14,12 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
buildTypes {
release {
@ -125,5 +131,4 @@ dependencies {
implementation 'com.googlecode.mp4parser:isoparser:1.1.21'
// Android常用库 https://github.com/l123456789jy/Lazy
implementation 'com.github.lazylibrary:lazylibrary:1.0.2'
}

View File

@ -1,7 +1,18 @@
package com.navinfo.outdoor.activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
@ -9,17 +20,45 @@ import com.hjq.permissions.XXPermissions;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.MessageDialog;
import com.lzy.okgo.model.HttpParams;
import com.navinfo.outdoor.R;
import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseActivity;
import com.kongzue.dialog.util.DialogSettings;
import com.navinfo.outdoor.bean.ApkVersionBean;
import com.navinfo.outdoor.http.Callback;
import com.navinfo.outdoor.http.HttpInterface;
import com.navinfo.outdoor.http.OkGoBuilder;
import com.navinfo.outdoor.util.APKVersionCodeUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
/**
* 启动页
*/
public class MainActivity extends BaseActivity {
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
if (msg.what==0){
//将下载进度对话框取消
pBar.cancel();
//安装apk也可以进行静默安装
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Constant.NAVINFO_APk, "navinfo.apk")),
"application/vnd.android.package-archive");
startActivityForResult(intent, 10);
}
return false;
}
});
private ProgressDialog pBar;
@Override
protected int getLayout() {
@ -33,6 +72,7 @@ public class MainActivity extends BaseActivity {
DialogSettings.style = (DialogSettings.STYLE.STYLE_KONGZUE);
DialogSettings.init();//初始化清空 BaseDialog 队列
}
private void initPermission() {
XXPermissions.with(this)
// 申请安装包权限
@ -61,19 +101,8 @@ public class MainActivity extends BaseActivity {
@Override
public void onGranted(List<String> permissions, boolean all) {
if (all) {
new CountDownTimer(3000, 1000) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
MainActivity.this.finish();
}
}.start();
}else {
initCheckVersion();
} else {
initPermission();
}
}
@ -82,7 +111,7 @@ public class MainActivity extends BaseActivity {
public void onDenied(List<String> permissions, boolean never) {
if (never) {
DialogSettings.style = DialogSettings.STYLE.STYLE_KONGZUE;
MessageDialog.show(MainActivity.this, "提示", permissions.toString()+" 被永久拒绝授权请手动授权,", "确定").setOkButton(new OnDialogButtonClickListener() {
MessageDialog.show(MainActivity.this, "提示", permissions.toString() + " 被永久拒绝授权请手动授权,", "确定").setOkButton(new OnDialogButtonClickListener() {
@Override
public boolean onClick(BaseDialog baseDialog, View v) {
XXPermissions.startPermissionActivity(MainActivity.this, permissions);
@ -95,6 +124,165 @@ public class MainActivity extends BaseActivity {
}
});
}
private void initCheckVersion() {
int versionCode = APKVersionCodeUtils.getVersionCode(this);//获取当前版本号
HttpParams httpParams = new HttpParams();
httpParams.put("version", versionCode);
httpParams.put("operationType", "android");
showLoadingDialog();
OkGoBuilder.getInstance()
.Builder(this)
.url(HttpInterface.APP_CHECK_VERSION)
.params(httpParams)
.cls(ApkVersionBean.class)
.token(Constant.ACCESS_TOKEN)
.getRequest(new Callback<ApkVersionBean>() {
@Override
public void onSuccess(ApkVersionBean response, int id) {
dismissLoadingDialog();
if (response.getCode() == 200) {
int version = response.getBody().getVersion();
if (versionCode < version) {
//升级
ApkVersionBean.bodyBean body = response.getBody();
showUpdateDialog(body);
} else {//不需要升级
initTime();
}
} else {
initTime();
}
}
@Override
public void onError(Throwable e, int id) {
dismissLoadingDialog();
Toast.makeText(MainActivity.this, e.getMessage()+"", Toast.LENGTH_SHORT).show();
initTime();
}
});
}
/**
* 显示升级信息的对话框
*/
private void showUpdateDialog(ApkVersionBean.bodyBean bodyBean) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setMessage("请升级APP版本至" + bodyBean.getVersion());
builder.setCancelable(false);
if (bodyBean.getUpdateState()==0){//非必须更新
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
downFile(bodyBean.getApkUrl());//点击确定将apk下载
} else {
Toast.makeText(MainActivity.this, "SD卡不可用请插入SD卡", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//用户点击了取消
}
});
}else{//强制更新
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
downFile(bodyBean.getApkUrl());//点击确定将apk下载
} else {
Toast.makeText(MainActivity.this, "SD卡不可用请插入SD卡", Toast.LENGTH_SHORT).show();
}
}
});
}
builder.create().show();
}
public void initTime() {
new CountDownTimer(3000, 1000) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
MainActivity.this.finish();
}
}.start();
}
/**
* 下载最新版本的apk
*
* @param path apk下载地址
*/
private void downFile(final String path) {
pBar = new ProgressDialog(this);
pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pBar.setCancelable(false);
pBar.setTitle("正在下载...");
pBar.setMessage("请稍候...");
pBar.setProgress(0);
pBar.show();
new Thread() {
public void run() {
try {
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(5000);
con.setConnectTimeout(5000);
con.setRequestProperty("Charset", "UTF-8");
con.setRequestMethod("GET");
if (con.getResponseCode() == 200) {
int length = con.getContentLength();// 获取文件大小
InputStream is = con.getInputStream();
pBar.setMax(length); // 设置进度条的总长度
FileOutputStream fileOutputStream = null;
if (is != null) {
//对apk进行保存
File file = new File(Constant.NAVINFO_APk, "navinfo.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch;
int process = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
process += ch;
pBar.setProgress(process); // 实时更新进度了
}
}
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
//apk下载完成使用Handler()通知安装apk
handler.sendEmptyMessage(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
@Override
protected void initView() {
super.initView();

View File

@ -35,6 +35,8 @@ public class Constant {
public static final String PICTURE_FOLDER = BASE_FOLDER + "/picture";
public static final String VIDEOS_ = BASE_FOLDER + "/videos";
public static final String POI_DAO = BASE_FOLDER + "/BaseDao/";
//下载文件
public static final String NAVINFO_APk = BASE_FOLDER + "/apk";
// 申请权限的RequestCode
public static final int PERMISSION_REQUEST_CODE = 0x100;

View File

@ -0,0 +1,96 @@
package com.navinfo.outdoor.bean;
public class ApkVersionBean {
private Integer code;
private String message;
private bodyBean body;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public bodyBean getBody() {
return body;
}
public void setBody(bodyBean body) {
this.body = body;
}
public static class bodyBean{
/**
*     "version": 155,
*         "operationType": null,
*         "apkUrl": "http://dtxbmaps.navinfo.com/apk/android/DTXB.apk",
*         "updateState": 0,
*         "releaseUrl": "http://dtxbmaps.navinfo.com/apk/android/155.txt",
*         "updateDate": "2021-05-07T03:31:35.357+0000"
*/
private int version;
private String operationType;
private String apkUrl;
private int updateState;
private String releaseUrl;
private String updateDate;
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getOperationType() {
return operationType;
}
public void setOperationType(String operationType) {
this.operationType = operationType;
}
public String getApkUrl() {
return apkUrl;
}
public void setApkUrl(String apkUrl) {
this.apkUrl = apkUrl;
}
public int getUpdateState() {
return updateState;
}
public void setUpdateState(int updateState) {
this.updateState = updateState;
}
public String getReleaseUrl() {
return releaseUrl;
}
public void setReleaseUrl(String releaseUrl) {
this.releaseUrl = releaseUrl;
}
public String getUpdateDate() {
return updateDate;
}
public void setUpdateDate(String updateDate) {
this.updateDate = updateDate;
}
}
}

View File

@ -47,6 +47,7 @@ import com.navinfo.outdoor.base.BaseDrawerFragment;
import com.navinfo.outdoor.base.BaseFragment;
import com.navinfo.outdoor.bean.JobSearchBean;
import com.navinfo.outdoor.bean.RoadExtend;
import com.navinfo.outdoor.bean.UserBean;
import com.navinfo.outdoor.http.Callback;
import com.navinfo.outdoor.http.DialogCallback;
import com.navinfo.outdoor.http.HttpInterface;
@ -207,6 +208,8 @@ public class TreasureFragment extends BaseFragment implements View.OnClickListen
showLoadingDialog();
//开启定位权限
checkNetWork();
//上传用户位置金纬度
iniUserLocation();
//检查是否有没有填完的页面
initSharePre();
//数据库
@ -335,6 +338,36 @@ public class TreasureFragment extends BaseFragment implements View.OnClickListen
treasureMap.getLayoutParams().height = widtHeight[1];
}
private void iniUserLocation() {
String encode = Geohash.getInstance().encode(Constant.currentLocation.getLatitude(), Constant.currentLocation.getLongitude());
HttpParams httpParams = new HttpParams();
httpParams.put("geom",encode);
OkGoBuilder.getInstance().Builder(getActivity())
.url(HttpInterface.USER_LOCATION)
.cls(UserBean.class)
.params(httpParams)
.token(Constant.ACCESS_TOKEN)
.postRequest(new Callback<UserBean>() {
@Override
public void onSuccess(UserBean response, int id) {
dismissLoadingDialog();
if (response.getCode()==200){
Toast.makeText(getActivity(), "成功", Toast.LENGTH_SHORT).show();
Log.d("TAG", "onSuccess: ssssssssssssssssssssssssss 成功");
}else {
Toast.makeText(getActivity(), response.getMessage()+"", Toast.LENGTH_SHORT).show();
Log.d("TAG", "onSuccess: "+response.getCode()+response.getMessage()+""+response.getBody());
}
}
@Override
public void onError(Throwable e, int id) {
dismissLoadingDialog();
Log.d("TAG", "onSuccess: sss********sssssssssssss 成功"+e.getMessage()+"");
}
});
}
private void initList(TencentLocation tencentLocation) {
int task_type = Constant.TASK_TYPE;
int limit_type = Constant.LIMIT_TTPE;

View File

@ -10,6 +10,14 @@ public class HttpInterface {
public static final String PRICE_PATH="/price/";//提现
public static final String TASK_PATH="/task/";//任务
public static final String USERID= Constant.USERID;
/**
* 用户版本升级
*/
//dtxbmaps.navinfo.com/dtxb/m4/user/appVersion/checkVersion?version=155&operationType=android
//172.23.139.4:8001/appVersion/checkVersion?version=155&operationType=android version是版本 operationType固定值 安卓 get
public static final String APKIP="http://172.23.139.4:8001/";
public static final String APP_CHECK_VERSION = IP+"appVersion/checkVersion"; //版本升级
/**
* 我的
* Path=/m4/user/*
@ -21,6 +29,10 @@ public class HttpInterface {
public static final String USER_BANKCARD_UP_DATA =IP +USER_PATH+ "user/m4/userBankcard/update"; //绑定银行卡
//172.23.139.4:8001/m4/userAuth/add
public static final String USER_AUTH_ADD = IP+USER_PATH+ "userAuth/add"; //实名认证
//172.23.139.4:9999/m4/user/userLocation/1/userLocation post 参数 geom:geohash加密
public static final String geomIP="http://172.23.139.4:9999/m4";
public static final String USER_LOCATION = geomIP+USER_PATH+ "userLocation/"+USERID+"/userLocation"; //上传用户坐标
/**
* 发现
* Path=/m4/msgList/**

View File

@ -0,0 +1,40 @@
package com.navinfo.outdoor.util;
import android.content.Context;
import android.content.pm.PackageManager;
public class APKVersionCodeUtils {
/**
*获取本地版本号
*
*/
public static int getVersionCode(Context mContext) {
int versionCode = 0;
try {
//获取软件版本号对应AndroidManifest.xml下android:versionCode
versionCode = mContext.getPackageManager().
getPackageInfo(mContext.getPackageName(), 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
/**
* 获取版本号名称
*
* @param context 上下文
* @return
*/
public static String getVerName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().
getPackageInfo(context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return verName;
}
}