From fb7a2b64bc31b08af5eb4b592affe0067642b8dd Mon Sep 17 00:00:00 2001 From: Zhao Date: Mon, 28 Jul 2025 17:22:19 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AB=98=E5=BE=B7=E5=AE=9A=E4=BD=8D=20?= =?UTF-8?q?=E7=AD=89=E5=BE=85=E6=8C=87=E7=A4=BA=E5=99=A8=20=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nsgk/ruralWeb/ui/SettingsFragment.java | 83 +++++++++++++++++-- app/src/main/res/xml/file_paths.xml | 7 ++ gradle.properties | 3 +- 3 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 app/src/main/res/xml/file_paths.xml diff --git a/app/src/main/java/com/nsgk/ruralWeb/ui/SettingsFragment.java b/app/src/main/java/com/nsgk/ruralWeb/ui/SettingsFragment.java index f3c4cd5..ad42f14 100644 --- a/app/src/main/java/com/nsgk/ruralWeb/ui/SettingsFragment.java +++ b/app/src/main/java/com/nsgk/ruralWeb/ui/SettingsFragment.java @@ -1,10 +1,14 @@ package com.nsgk.ruralWeb.ui; +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; @@ -15,6 +19,7 @@ import android.util.Log; import android.widget.Toast; import androidx.appcompat.app.AlertDialog; +import androidx.core.app.NotificationCompat; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceManager; @@ -33,6 +38,7 @@ import java.io.File; import java.util.Objects; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.StreamProgress; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpUtil; @@ -250,29 +256,94 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Prefer Log.d(ID_TAG, "下载apk地址: " + updateUrl); Toast.makeText(context, "开始下载......", Toast.LENGTH_SHORT).show(); + int icon = context.getResources().getIdentifier("ic_launcher_" + NSConstants.AppIcon()/* + "_round"*/, "mipmap", context.getApplicationContext().getPackageName()); + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + + String channelId = "nsgk"; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId); + if(null == notificationChannel) + { + NotificationChannel channel = new NotificationChannel(channelId, "下载更新", NotificationManager.IMPORTANCE_DEFAULT); + channel.setDescription("下载更新"); + notificationManager.createNotificationChannel(channel); + } + } + + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId); + builder.setProgress(0, 0, true) + .setSmallIcon(icon) + .setTicker("下载更新") + .setOngoing(true) + .setWhen(System.currentTimeMillis()) + .setContentTitle("下载更新") + .setContentText("开始下载......") + ; + int notifyId = 0x1003; + Notification notification = builder.build(); + notificationManager.notify(notifyId, notification); + // 创建文件夹并删除缓存文件 File externalFilesDir = context.getExternalFilesDir(null); String dir = externalFilesDir.getAbsolutePath() + File.separator + NSConstants.APK_UPDATE_DOWNLOAD_DIR; + Log.d(ID_TAG, "创建下载缓存目录: " + dir); + notificationManager.notify(notifyId, builder.setContentText("创建下载缓存目录").build()); FileUtil.mkdir(dir); + String file = dir + File.separator + NSConstants.APK_UPDATE_DOWNLOAD_FILE; - Log.d(ID_TAG, "删除文件: " + file); + Log.d(ID_TAG, "删除历史缓存文件: " + file); + notificationManager.notify(notifyId, builder.setContentText("删除历史缓存文件").build()); FileUtil.del(file); new Thread(() -> { - long bytes = HttpUtil.downloadFile(updateUrl, file); + long bytes = HttpUtil.downloadFile(updateUrl, new File(file), new StreamProgress() { + @Override + public void start() + { + handler.post(() -> { + notificationManager.notify(notifyId, builder.setContentText("开始下载......").setProgress(100, 0, false).build()); + }); + } + + @Override + public void progress(long progressSize) + { + handler.post(() -> { + notificationManager.notify(notifyId, builder.setContentText("下载: " + FileUtil.readableFileSize(progressSize)).setProgress(0, 0, true).build()); + }); + } + + @Override + public void finish() + { + handler.post(() -> { + notificationManager.notify(notifyId, builder.setContentText("下载完成").setProgress(100, 100, false).setOngoing(false).build()); + }); + } + }); Log.d(ID_TAG, "下载apk文件: " + FileUtil.readableFileSize(bytes)); if(bytes > 0) { - handler.post(() -> { + handler.postDelayed(() -> { + notificationManager.notify(notifyId, builder.setContentText("准备安装更新").setProgress(0, 0, false).setOngoing(false).build()); Toast.makeText(context, "准备安装......", Toast.LENGTH_SHORT).show(); NSContextUtils.InstallApk(context, file); - }); + +// handler.postDelayed(() -> { +// notificationManager.cancel(notifyId); +// }, 2000); + }, 1000); } else { - handler.post(() -> { + handler.postDelayed(() -> { + notificationManager.notify(notifyId, builder.setContentText("下载更新失败").setProgress(0, 0, false).setOngoing(false).build()); Toast.makeText(context, "下载更新失败", Toast.LENGTH_LONG).show(); - }); + +// handler.postDelayed(() -> { +// notificationManager.cancel(notifyId); +// }, 2000); + }, 1000); } }).start(); } diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml new file mode 100644 index 0000000..c4dd1c9 --- /dev/null +++ b/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index aec370e..c308b18 100644 --- a/gradle.properties +++ b/gradle.properties @@ -34,6 +34,7 @@ appCopyright= # [String] App vendor name, it will save to @string/app_vendor. default using @string/company appVendor= # [String] App update download url, it will save to @string/app_update_url -appUpdateUrl=http://218.59.175.43:8090/nsgk/qixingguan.apk +# http://218.59.175.43:8090/nsgk/qixingguan.apk +appUpdateUrl= # gaode amap key amapKey=490bef43ef0182379aa1a8bacc45d054 \ No newline at end of file