android - Android静默更新apk然后重启应用
问题描述
好的,首先我想澄清一下,我并不想做任何可疑的事情。我们有自己的企业应用程序,仅与我们自己的硬件配套使用(我们没有使用 Google Play 商店)。手机也root了。我已经实现了我们自己的 Apk 更新机制。到目前为止,我已经使用下面的代码成功地安装了 apk
try {
val command = "pm install -r " + file.path
val openAppCommand = "am start -a android.intent.action.MAIN -n" +
BuildConfig.APPLICATION_ID + "/.MainActivity"
val process = Runtime.getRuntime().exec(arrayOf("su", "-c", command, openAppCommand))
val exitVal = process.waitFor()
if (process.exitValue() == 0) {
Log.e("updateAppSilently", "Apk installed")
} else {
Log.e("updateAppSilently", "Something went wrong while installing apk")
}
} catch (e: Exception) {
e.printStackTrace()
}
openAppCommand 被忽略,因为重启后当前进程被杀死。
我什至尝试过
<receiver android:name="com.updatesmanager.AppUpdateBroadcastReceiver"
android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
和类文件
class AppUpdateBroadcastReceiver : BroadcastReceiver(){
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("AppUpdateBroadcastReceiver", "App got updated!")
val intent = Intent(context, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context?.startActivity(intent)
/* Log.d("AppUpdateBroadcastReceiver", "App got updated!")
val command = "am start -a android.intent.action.MAIN -n" +
BuildConfig.APPLICATION_ID + "/.MainActivity"
val process = Runtime.getRuntime().exec(arrayOf("su", "-c", command))
val exitVal = process.waitFor()
if(exitVal == 0){
Log.e("AppUpdateBroadcastReceiver", "App launched")
}*/
}
}
我什至尝试设置警报,但它不起作用,因为应用程序已更新/重新安装,因此警报被清除。
任何形式的帮助都将受到高度赞赏。
解决方案
好的,虽然这很愚蠢,但我没有更新更新 apk 的 versionCode(尽管我坚信广播接收器应该在pm install -r
运行命令时触发,而不考虑 versionCode,因为包已被替换)。当我从当前 apk 增加 versionCode 时。AppUpdateBroadcastReceiver 确实被触发了。
推荐阅读
- sql-server - 如果表中存在数据,则使用 SP 检索信息或生成信息
- kotlin - 如何启用 Firebase 实时数据库离线持久性?
- python - 将风向字典映射到新列
- arrays - rails fields_for number_field 数组必须具有不同的值
- python - 计算有权访问文件夹 python 的用户数
- cmake - 将库从一个子模块延迟链接到另一个子模块
- django - 如何使用 Django 注释?
- javascript - JavaScript 中 PHP 变量的使用
- python - 我的“更改密码”功能不起作用,而是陷入循环
- flutter - 有人在 Flutter 中呼叫用户后,我可以发送用户通知吗?