mirror of
https://github.com/yuzu-emu/yuzu-android.git
synced 2025-09-05 16:53:23 +00:00
Update UpdateManager.kt
This commit is contained in:
parent
a20ccffab6
commit
e110aa6c2b
|
@ -52,8 +52,8 @@ object UpdateManager {
|
||||||
if (responseBody != null) {
|
if (responseBody != null) {
|
||||||
val jsonObject = JSONObject(responseBody)
|
val jsonObject = JSONObject(responseBody)
|
||||||
val versionName = jsonObject.getString("versionName")
|
val versionName = jsonObject.getString("versionName")
|
||||||
val title = jsonObject.optString("title", "")
|
val title = jsonObject.optString("title", "发现新版本")
|
||||||
val message = jsonObject.optString("message", "")
|
val message = jsonObject.optString("message", "有新版本可用,是否立即更新?")
|
||||||
VersionInfo(versionName, title, message)
|
VersionInfo(versionName, title, message)
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
|
@ -83,72 +83,5 @@ object UpdateManager {
|
||||||
}
|
}
|
||||||
.show()
|
.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showNoUpdateAvailableMessage(context: Context) {
|
|
||||||
Toast.makeText(context, "您的应用已经是最新版本。", Toast.LENGTH_SHORT).show()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun downloadAndInstallUpdate(context: Context) {
|
|
||||||
val downloadUrl = "https://your-server.com/api/downloadLatestVersion" // 替换为实际的下载链接
|
|
||||||
|
|
||||||
val request = DownloadManager.Request(Uri.parse(downloadUrl))
|
|
||||||
request.setTitle("App更新")
|
|
||||||
request.setDescription("正在下载新版本...")
|
|
||||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
|
||||||
request.setDestinationInExternalPublicDir(
|
|
||||||
Environment.DIRECTORY_DOWNLOADS,
|
|
||||||
"yuzu.apk"
|
|
||||||
)
|
|
||||||
|
|
||||||
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
|
||||||
val downloadId = downloadManager.enqueue(request)
|
|
||||||
|
|
||||||
// 您可以存储下载ID并用于检查下载状态或处理重试
|
|
||||||
|
|
||||||
// 当下载完成时安装下载的APK
|
|
||||||
val onCompleteReceiver = DownloadCompleteReceiver()
|
|
||||||
onCompleteReceiver.setDownloadId(downloadId)
|
|
||||||
context.registerReceiver(
|
|
||||||
onCompleteReceiver,
|
|
||||||
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class DownloadCompleteReceiver : BroadcastReceiver() {
|
|
||||||
private var downloadId: Long = -1
|
|
||||||
|
|
||||||
fun setDownloadId(id: Long) {
|
|
||||||
downloadId = id
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
|
||||||
if (context == null || intent == null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (intent.action == DownloadManager.ACTION_DOWNLOAD_COMPLETE) {
|
|
||||||
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
|
||||||
val query = DownloadManager.Query().setFilterById(downloadId)
|
|
||||||
val cursor = downloadManager.query(query)
|
|
||||||
|
|
||||||
if (cursor.moveToFirst()) {
|
|
||||||
val status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
|
|
||||||
if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
|
||||||
val uri = Uri.parse(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)))
|
|
||||||
installApk(context, uri)
|
|
||||||
} else {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun installApk(context: Context, uri: Uri) {
|
|
||||||
val intent = Intent(Intent.ACTION_VIEW)
|
|
||||||
intent.setDataAndType(uri, "application/vnd.android.package-archive")
|
|
||||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
|
||||||
context.startActivity(intent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue