wjc
2025-05-07 5d3efa4c93dde0cde474951e5310bb72ebbf4184
app/src/main/java/com/hdl/photovoltaic/widget/apkwgtupload/AppDownloadManager.java
@@ -87,7 +87,7 @@
            request.setDescription(desc);
            // 完成后显示通知栏
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalFilesDir(weakReference.get(), Environment.DIRECTORY_DOWNLOADS, HdlAppUpdateLogic.ApkFileName);
            request.setDestinationInExternalFilesDir(weakReference.get(), Environment.DIRECTORY_DOWNLOADS, HdlAppUpdateLogic.getInstance().getApkFileName());
            request.setMimeType("application/vnd.android.package-archive");
        } else {
            request.setDestinationInExternalFilesDir(weakReference.get(), Environment.DIRECTORY_DOWNLOADS, HDLUniMP.UNI_APP_ID + ".wgt");
@@ -110,8 +110,11 @@
        //设置监听Uri.parse("content://downloads/my_downloads")
        weakReference.get().getContentResolver().registerContentObserver(Uri.parse("content://downloads/my_downloads"), true,
                mDownLoadChangeObserver);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        // 注册广播,监听APK是否下载完成
        weakReference.get().registerReceiver(mDownloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        weakReference.get().registerReceiver(mDownloadReceiver, intentFilter);
    }
    /**
@@ -148,7 +151,8 @@
            downSuccess = true;
            Log.i(TAG, "下载进度:完成");
        }
        Log.i(TAG, "下载进度:" + bytesAndStatus[0] + "/" + bytesAndStatus[1] + "");
        long value = bytesAndStatus[0] * 100L / bytesAndStatus[1];
        Log.i(TAG, "下载进度:" + value + "");
    }
    class DownloadChangeObserver extends ContentObserver {
@@ -192,16 +196,16 @@
                                @Override
                                public void permissionFail() {
                                    HdlThreadLogic.toast(HDLApp.getInstance(), HDLApp.getInstance().getResources().getString(R.string.app_update_fail));
                                    HdlThreadLogic.toast(HDLApp.getInstance(), HDLApp.getInstance().getAppLocaleContext().getString(R.string.app_update_fail));
                                }
                            };
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    AndroidPermissionActivity.sListener = listener;
                                    Intent intent1 = new Intent(context, AndroidPermissionActivity.class);
                                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    context.startActivity(intent1);
                                    Intent intentPermission = new Intent(context, AndroidPermissionActivity.class);
                                    intentPermission.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    context.startActivity(intentPermission);
                                }
                            }, 2000);
@@ -221,8 +225,8 @@
    /**
     * @param context
     * @param intent
     * @param context -
     * @param intent  -
     */
    private void installApk(Context context, Intent intent) {
        long completeDownLoadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
@@ -235,7 +239,7 @@
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { // 6.0以下
                uri = mDownloadManager.getUriForDownloadedFile(completeDownLoadId);
                if (null == uri) {
                    File apkFile = new File(weakReference.get().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), HdlAppUpdateLogic.ApkFileName);
                    File apkFile = new File(weakReference.get().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), HdlAppUpdateLogic.getInstance().getApkFileName());
                    if (apkFile.exists()) {
                        uri = Uri.fromFile(apkFile);
                    }
@@ -245,8 +249,8 @@
                uri = Uri.fromFile(apkFile);
            } else { // Android 7.0 以上
                uri = FileProvider.getUriForFile(context,
                        "com.hdl.debugtreasure.dc.fileprovider",
                        new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), HdlAppUpdateLogic.ApkFileName));
                        "com.hdl.photovoltaic.fileProvider",
                        new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), HdlAppUpdateLogic.getInstance().getApkFileName()));
                intentInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
            // 安装应用