我创建了一个设备所有者应用程序,该应用程序在启动时固定该应用程序,我想从 GitHub 中托管的 apk(而不是 PlayStore)更新 Kiosk 启动器应用程序,我已成功下载 apk 文件,但当尝试以编程方式安装 apk 确认弹出窗口以进行更新。是否可以在无需用户确认的情况下静默更新应用程序?
我尝试使用此代码
override fun doInBackground(vararg p0: String?): Boolean {
var flag = false
try {
val path =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/"
var outputFile = File("$path$fileName.apk")
var repetition = 1
while (outputFile.exists()) {
outputFile = File("$path$fileName ($repetition).apk")
repetition++
}
val directory = File(path)
if (!directory.exists()) {
directory.mkdirs()
}
val url = URL(downloadUrl)
val c = url.openConnection() as HttpURLConnection
c.requestMethod = "GET"
c.connect()
val fos = FileOutputStream(outputFile)
val inputStream = c.inputStream
val totalSize = c.contentLength.toFloat() // size of apk
val buffer = ByteArray(1024)
var len1: Int
var per: Float
var downloaded = 0f
while (inputStream.read(buffer).also { len1 = it } != -1) {
fos.write(buffer, 0, len1)
downloaded += len1
per = (downloaded * 100 / totalSize)
publishProgress(per.toInt())
}
fos.close()
// inputStream.close()
// openNewVersion(outputFile.path)
installPackage(context, inputStream, "com.nurse.carenurse")
flag = true
} catch (e: MalformedURLException) {
Log.e("DownloadApk", "Update Error: " + e.message)
flag = false
} catch (e: IOException) {
e.printStackTrace()
}
return flag
}
fun installPackage(context: Context, `in`: InputStream, packageName: String?): Boolean {
val packageInstaller = context.packageManager.packageInstaller
val params = SessionParams(SessionParams.MODE_FULL_INSTALL)
params.setAppPackageName(packageName)
// set params
val sessionId = packageInstaller.createSession(params)
val session = packageInstaller.openSession(sessionId)
val out = session.openWrite("TestDPC", 0, -1)
val buffer = ByteArray(65536)
var c: Int
while (`in`.read(buffer).also { c = it } != -1) {
out.write(buffer, 0, c)
}
session.fsync(out)
`in`.close()
out.close()
session.commit(createIntentSender(context, sessionId))
return true
}
private fun createIntentSender(context: Context, sessionId: Int): IntentSender {
val ACTION_INSTALL_COMPLETE = "com.nurse.carenurse.ACTION_INSTALL_COMPLETE"
val pendingIntent = PendingIntent.getBroadcast(
context,
sessionId,
Intent(ACTION_INSTALL_COMPLETE),
PendingIntent.FLAG_IMMUTABLE
)
return pendingIntent.intentSender
}
我一直在处理同样的问题并来到这里。
我尝试过几个解决方案,包括与您类似的解决方案,但没有任何效果(总是弹出带有“取消”和“安装”按钮的窗口),除了 SimpleInstaller 版本 5.0.0(我知道它已存档并移至 Ackpine,但 Ackpine 也向我显示了弹出窗口)
首先,作为设备所有者,我已禁用 Play Protect,因为我在 Google Play 上没有我的应用程序,并且 Play Protect 完全阻止更新我的应用程序,尽管我拥有设备所有者权利。
梯度:
dependencies: {
implementation "io.github.solrudev:simpleinstaller:5.0.0"
}
我正在更新CoroutineWorker
内的应用程序val apk = File(applicationContext.filesDir.absolutePath + "/app-release.apk")
val installResult = PackageInstaller.installPackage(apk) {
confirmationStrategy = ConfirmationStrategy.IMMEDIATE
}
if(installResult is InstallResult.Success) {
// ...
}else if(installResult is InstallResult.Failure) {
val errorMessage = installResult.cause
// ...
}