Android : restart application after update - ACTION_PACKAGE_REPLACED

后端 未结 5 1437
情歌与酒
情歌与酒 2020-12-03 10:09

My application that is not on Play Store verify on the web If there are a new version and download and start it. After the installation I would like to restart the applicati

5条回答
  •  一生所求
    2020-12-03 11:03

    After hours and hours of searching how to restart your app after it was updated, I finally find why it won't restart.

    The app must NOT be launched by Android Studio or other IDE. If I manually install app with its apk and launch it from the current Android device, the app can recognize if there was an update of my application and it restarts correctly.

    My user case is a custom launcher that can update by itself launching a PackageInstaller.Session without going to Google Play Store

    Here it's the code

    Manifest:

    
         
             
         
    
    

    UpdateReceiver (Kotlin code):

    class UpdateReceiver: BroadcastReceiver() {
    
        companion object {
            private val TAG = UpdateReceiver::class.java.simpleName
        }
    
        override fun onReceive(context: Context?, intent: Intent?) {
            Log.d(TAG, "onReceive ${intent?.action ?: "unknown action"}")
            if (intent?.action == Intent.ACTION_MY_PACKAGE_REPLACED) {
              
                //MainActivity = Your first activity
                val yourIntent = Intent(context, MainActivity::class.java)
                yourIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                context?.startActivity(yourIntent)
            }
    
        }
    }
    

提交回复
热议问题