1.申請(qǐng)權(quán)限
? ? <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
? ? <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
? ? ? PermissionsUtil.checkAndRequestPermissions(this);
2.FileProvider存儲(chǔ)配置
? ? ? ? <provider
? ? ? ? ? ? android:name="androidx.core.content.FileProvider"
? ? ? ? ? ? android:authorities="${applicationId}.fileprovider"
? ? ? ? ? ? android:exported="false"
? ? ? ? ? ? android:grantUriPermissions="true">
? ? ? ? ? ? <meta-data
? ? ? ? ? ? ? ? android:name="android.support.FILE_PROVIDER_PATHS"
? ? ? ? ? ? ? ? android:resource="@xml/file_paths" />
? ? ? ? </provider>
xml/file_paths
<?xml version="1.0" encoding="utf-8"?>
<paths>
? ? <external-path name="external_Files" path="."/>
</paths>
3.數(shù)據(jù)請(qǐng)求
? ? private void updateApp() {
? ? ? ? String deviceId = getDeviceId();
? ? ? ? String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
? ? ? ? String token = SharedPreUtil.getInstance(mContext).getString(Constant.USER_TOKEN);
? ? ? ? String version = getVersionName(mContext);
? ? ? ? LogUtil.i("檢測(cè)版本參數(shù) token="+token);
? ? ? ? http.post().url(Constant.BASE_URL + Constant.UPDATE_APP)
? ? ? ? ? ? ? ? .addHeader("token", token)
? ? ? ? ? ? ? ? .addHeader("app_id", Constant.APP_ID)
? ? ? ? ? ? ? ? .addHeader("interface_version", version)
? ? ? ? ? ? ? ? .addHeader("device_id", deviceId)
? ? ? ? ? ? ? ? .addHeader("os", "android")
? ? ? ? ? ? ? ? .addHeader("timestamp", timestamp)
? ? ? ? ? ? ? ? .addHeader("Content-Type", "application/json")
? ? ? ? ? ? ? ? .tag(this).enqueue(new JsonResponseHandler() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onSuccess(int statusCode, JSONObject response) {
? ? ? ? ? ? ? ? LogUtil.d("檢測(cè)版本=" + response);
? ? ? ? ? ? ? ? int code = response.optInt("code");
? ? ? ? ? ? ? ? String message = response.optString("message");
? ? ? ? ? ? ? ? String data = response.optString("data");
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? JSONObject _data = new JSONObject(data);
? ? ? ? ? ? ? ? ? ? if (code == 1023) {//下載新版本
? ? ? ? ? ? ? ? ? ? ? ? updateDescription = _data.optString("appDescriptoin");
? ? ? ? ? ? ? ? ? ? ? ? String forceUpdate = _data.optString("forceUpdate");
? ? ? ? ? ? ? ? ? ? ? ? if (forceUpdate.equals("true")) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? isForceUpdate = 1;
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? isForceUpdate = 2;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? String downloadUrl = _data.optString("downloadUrl");
? ? ? ? ? ? ? ? ? ? ? ? if (downloadUrl.contains("\\")) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? downloadUrl = downloadUrl.replace("\\", "");
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? if (SharedPreUtil.getInstance(mContext).getInt(Constant.IS_DOWNLOAD_APP) != 1) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? downloadApp(downloadUrl);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? } else if (code == 1024) {//版本異常
? ? ? ? ? ? ? ? ? ? } else if (code == 200) {//最新版本
? ? ? ? ? ? ? ? ? ? ? ? ToastUtil.show(mContext, message);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onFailure(int statusCode, String error_msg) {
? ? ? ? ? ? ? ? LogUtil.d("檢測(cè)版本 onFailure=" + error_msg);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private void downloadApp(String url) {
//? ? ? ? downloadPath = Environment.getExternalStorageDirectory() + "/Download/GollumPay/GollumPay_" + System.currentTimeMillis() + ".apk";
? ? ? ? downloadPath = Environment.getExternalStorageDirectory() + "/Download/ChatRoom/ChatRoom_" + System.currentTimeMillis() + ".apk";
? ? ? ? LogUtil.i("檢測(cè)版本=" + url + " downloadPath=" + downloadPath);
? ? ? ? http.download()
? ? ? ? ? ? ? ? .url(url)
? ? ? ? ? ? ? ? .filePath(downloadPath)
? ? ? ? ? ? ? ? .tag(this)
? ? ? ? ? ? ? ? .enqueue(new DownloadResponseHandler() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onStart(long totalBytes) {
? ? ? ? ? ? ? ? ? ? ? ? LogUtil.i("更新app 開(kāi)始下載");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onFinish(File downloadFile) {
? ? ? ? ? ? ? ? ? ? ? ? File file = new File(downloadPath);
? ? ? ? ? ? ? ? ? ? ? ? SharedPreUtil.getInstance(mContext).saveParam(Constant.SAVE_DOWNLOAD_APP_PATH, downloadPath);
? ? ? ? ? ? ? ? ? ? ? ? SharedPreUtil.getInstance(mContext).saveParam(Constant.IS_DOWNLOAD_APP, 1);
//? ? ? ? ? ? ? ? ? ? ? ? String downloadMd5 = getFileMD5(file);
? ? ? ? ? ? ? ? ? ? ? ? LogUtil.i("更新app 下載完成=" + isForceUpdate /*+ " downloadMd5=" + downloadMd5 + " md5=" + md5*/);
? ? ? ? ? ? ? ? ? ? ? ? if (isForceUpdate == 1 /*&& downloadMd5.equals(md5)*/) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? updateDialog(updateDescription, file);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onProgress(long currentBytes, long totalBytes) {
? ? ? ? ? ? ? ? ? ? ? ? int a = (int) ((100 * currentBytes) / totalBytes);
? ? ? ? ? ? ? ? ? ? ? ? LogUtil.i("更新app 下載進(jìn)度=" + a + "%" + totalBytes);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onFailure(String error_msg) {
? ? ? ? ? ? ? ? ? ? ? ? LogUtil.i("更新app 下載失敗");//1.外部存儲(chǔ)的讀取權(quán)限未獲取
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? }
? ? private void updateDialog(String updateContext, final File file) {
? ? ? ? LogUtil.i("更新app 彈窗");
? ? ? ? final UpdateAppDialog myDialog = new UpdateAppDialog(mContext);
? ? ? ? myDialog.setContent(updateContext);
? ? ? ? myDialog.show();
? ? ? ? myDialog.setAttributes();//需先顯示,然后才能查找控件
? ? ? ? myDialog.setOnUpdateListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? installApk(file);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private void installApk(File file) {
? ? ? ? if (!file.exists()) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? Intent intent = new Intent(Intent.ACTION_VIEW);
? ? ? ? intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
? ? ? ? ? ? Uri apkUri = FileProvider.getUriForFile(mContext, "${applicationId}.fileprovider", file);
? ? ? ? ? ? intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
? ? ? ? ? ? intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
? ? ? ? } else {
? ? ? ? ? ? intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
? ? ? ? }
? ? ? ? mContext.startActivity(intent);
? ? }
? ? private String getVersionName(Context context) {
? ? ? ? PackageManager packageManager = context.getPackageManager();
? ? ? ? PackageInfo packageInfo;
? ? ? ? String versionName = "";
? ? ? ? try {
? ? ? ? ? ? packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
? ? ? ? ? ? versionName = packageInfo.versionName;
? ? ? ? } catch (PackageManager.NameNotFoundException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return versionName;
? ? }