瀏覽代碼

高德定位 等待指示器 下载更新

master
Zhao 2 天之前
父節點
當前提交
fb7a2b64bc
共有 3 個文件被更改,包括 86 次插入7 次删除
  1. +77
    -6
      app/src/main/java/com/nsgk/ruralWeb/ui/SettingsFragment.java
  2. +7
    -0
      app/src/main/res/xml/file_paths.xml
  3. +2
    -1
      gradle.properties

+ 77
- 6
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();
}


+ 7
- 0
app/src/main/res/xml/file_paths.xml 查看文件

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/com.nsgk.ruralWeb" name="external_root"/>
<external-path path="nsgk/ruralWeb" name="ruralweb_root"/>
<external-path path="nsgk" name="nsgk_root"/>
<external-path path="." name="external_storage_root"/>
</paths>

+ 2
- 1
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

Loading…
取消
儲存