• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

Android实现热更新

Android cps12345 7个月前 (07-08) 165次浏览 0个评论

什么是应用内更新:


弹性更新:

优点:

灵活更新的示例:Android实现热更新

即时更新:

即时更新示例:Android实现热更新

实现方式:

步骤1:添加依赖

implementation'com.google.android.play:core:1.6.4'

步骤2:建立AppManager执行个体

// Creates instance of the manager.
private val appUpdateManager: AppUpdateManager? = AppUpdateManagerFactory.create(activity)

步骤3:检查更新

private fun checkUpdate() { 
	// Returns an intent object that you use to check for an update.    val appUpdateInfoTask = appUpdateManager?.appUpdateInfo 
	// Checks that the platform will allow the specified type of update.
	Log.d(TAG, "Checking for updates") 
	appUpdateInfoTask?.addOnSuccessListener { 
		appUpdateInfo -> if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)){
		// Request the update. 
			Log.d(TAG, "Update available") 
		} else { 
			Log.d(TAG, "No Update available") 
		} 

	} 

}

步骤4:请求更新

appUpdateManager.startUpdateFlowForResult(
	// Pass the intent that is returned by 'getAppUpdateInfo()'.
	appUpdateInfo,
	// Or 'AppUpdateType.FLEXIBLE' for flexible updates.
	AppUpdateType.FLEXIBLE,
	// The current activity making the update request.
	this,
	// Include a request code to later monitor this update request.
	MY_REQUEST_CODE)

步骤5:监控更新

单击更新按钮后,更新将在后台开始,下载开始后,您需要在可以安装更新时监视更新并在应用程序的UI中显示进度。因此,我们需要注册侦听器以获取状态更新。像下面的代码片段:

private val listener: InstallStateUpdatedListener? = InstallStateUpdatedListener { 

installState ->if (installState.installStatus() == InstallStatus.DOWNLOADED) {

// After the update is downloaded, show a notification

// and request user confirmation to restart the app.

Log.d(TAG, "An update has been downloaded")

showSnackBarForCompleteUpdate()

}

}

在上面的代码中,InstallStateUpdateListener 会为我们提供状态,当状态等于“已下载”时,我们需要向用户显示通知以重新启动应用程序。

注册听众

// Before starting an update, register a listener for updates.
appUpdateManager.registerListener(listener)

取消注册监听器

// When status updates are no longer needed, unregister the listener.
appUpdateManager.unregisterListener(listener)

注意:仅灵活下载才需要监视更新状态

步骤6:完成“弹性模式”更新

appUpdateManager !!。completeUpdate()

步骤7:监视即时模式更新

// Checks that the update is not stalled during 'onResume()'.
// However, you should execute this check at all entry points into the app.
override fun onResume() {
    super.onResume()

    appUpdateManager
        .appUpdateInfo
        .addOnSuccessListener { appUpdateInfo ->
            ...
            if (appUpdateInfo.updateAvailability()
                == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS
            ) {
                // If an in-app update is already running, resume the update.
                appUpdateManager.startUpdateFlowForResult(
                    appUpdateInfo,
                    IMMEDIATE,
                    this,
                    MY_REQUEST_CODE
                );
            }
        }
}

步骤8:处理取消更新。

fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    // super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == InAppUpdateConstant.REQUEST_CODE) {
        when (resultCode) {
            Activity.RESULT_OK -> {
                Log.d(TAG, "" + "Result Ok")
                //  handle user's approval }
            }
            Activity.RESULT_CANCELED -> {
                {                           //if you want to request the update again just call checkUpdate()
                }
                Log.d(TAG, "" + "Result Cancelled")
                //  handle user's rejection  }
            }
            ActivityResult.RESULT_IN_APP_UPDATE_FAILED -> {
                //if you want to request the update again just call checkUpdate()
                Log.d(TAG, "" + "Update Failure")
                //  handle update failure
            }
        }
    }
}

注意:该对话框将仅在生产版本中显示,而不在Beta版本中显示。

测试方法:

第1部分

第2部分


每次上传Build时,请确保关闭Google Play商店应用

喜欢 (2)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址