| | |
| | | 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"); |
| | |
| | | //设置监听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); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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 { |
| | |
| | | @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); |
| | | |
| | |
| | | |
| | | |
| | | /** |
| | | * @param context |
| | | * @param intent |
| | | * @param context - |
| | | * @param intent - |
| | | */ |
| | | private void installApk(Context context, Intent intent) { |
| | | long completeDownLoadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); |
| | |
| | | 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); |
| | | } |
| | |
| | | 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); |
| | | } |
| | | // 安装应用 |