我尝试在我的应用程序之一中实现应用内更新功能。我集成了应用内代码片段,但是当我运行代码时,它会抛出一条错误消息。
代码 :
class MainActivity : AppCompatActivity() {
private val REQUEST_CODE: Int = 123
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initInAppUpdate()
}
private lateinit var appUpdateManager: AppUpdateManager
private lateinit var listener: InstallStateUpdatedListener
private fun initInAppUpdate() {
// Create instance of the IAUs manager.
appUpdateManager = AppUpdateManagerFactory.create(this)
// Add state listener to app update info task.
appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
// If there is an update available, prepare to promote it.
Log.d("TAG","====>${appUpdateInfo.updateAvailability()}")
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
appUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,
// Or 'AppUpdateType.IMMEDIATE for immediate updates.
AppUpdateType.FLEXIBLE,
// The current activity.
this,
REQUEST_CODE
)
}
// If the process of downloading is finished, start the completion flow.
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
Log.d("TAG","====> InstallStatus.DOWNLOADED")
}
}
.addOnFailureListener { e ->
Log.d("TAG","====> Failed : ${e.printStackTrace()}")
}
// Create a listener to track downloading state updates.
listener = InstallStateUpdatedListener { state ->
// Update progress indicator, request user to approve app reload, etc.
Log.d("TAG","====> Current State : $state")
}
// At some point before starting an update, register a listener for updates.
appUpdateManager.registerListener(listener)
}
override fun onDestroy() {
super.onDestroy()
// At some point when status updates are no longer needed, unregister the listener.
appUpdateManager.unregisterListener(listener)
}
}
错误 :
com.google.android.play.core.internal.aa: Failed to bind to the service.
at com.google.android.play.core.internal.q.b(Unknown Source:82)
at com.google.android.play.core.internal.q.a(Unknown Source:0)
at com.google.android.play.core.internal.s.a(Unknown Source:4)
at com.google.android.play.core.internal.r.run(Unknown Source:0)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.os.HandlerThread.run(HandlerThread.java:65)