Android静默更新apk然后重启app [英] Android silently updating apk and then restart the app

查看:84
本文介绍了Android静默更新apk然后重启app的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,首先我想澄清一下,我不是想达到任何可疑的目的.我们有自己的企业应用程序,只能与我们自己的硬件搭配使用(我们没有使用 Google Play 商店).手机也root了.我已经实现了我们自己的 Apk 更新机制.到目前为止,我已经使用下面的代码成功地静默安装了 apk

Okay first of all I want to clear, I'm not trying to achieve anything fishy. We have our own enterprise app that only goes with our own hardware (we are not using Google play store). Also the phone is rooted. I have implemented our own Apk update mechanism. So far I have successfully silently installed apk using the below code

    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 被忽略,因为在重新启动后当前进程被终止.

openAppCommand is getting ignore because after restart the current process is killed.

我什至尝试过

       <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")
        }*/
    }

}

我什至尝试过设置警报,但它不起作用,因为应用程序已更新/重新安装,因此警报被清除.

I have even tried setting alarm but it will not work because the app is updated/reinstall so the alarms gets cleared.

非常感谢任何形式的帮助.

Any kind of help is highly appreciated.

推荐答案

好吧,虽然这很愚蠢,但我没有更新更新 apk 的 versionCode(尽管我坚信广播接收器应该在 pm install -r 命令的运行与 versionCode 无关,因为包被替换了).当我从当前的 apk 增加 versionCode 时.AppUpdateBroadcastReceiver 确实被触发了.

Okay, that's silly though but I was not updating the versionCode of the update apk (Although I strongly believe that the broadcast receiver should trigger in case when pm install -r command is ran regardless of the versionCode, because the package is replaced). When I increased the versionCode from the current apk. The AppUpdateBroadcastReceiver did got triggered.

这篇关于Android静默更新apk然后重启app的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆