APP升級(jí)

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;

? ? }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容