2

我已经完成了此处提到的步骤Support in-app updates。我已将应用程序上传到 Play 商店以检查应用程序更新功能。下面是我的代码片段:

class MainActivity : AppCompatActivity() {

    val REQUEST_CODE_UPDATE = 1001

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        fab.setOnClickListener { view ->
           Snackbar.make(view, "Checking for update", Snackbar.LENGTH_LONG)
                .show()
            checkForUpdate()
        }

    }


    override fun onResume() {
        super.onResume()

        val updateManager = AppUpdateManagerFactory.create(this)
        updateManager.appUpdateInfo
            .addOnSuccessListener {
                if (it.updateAvailability() ==
                    UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                    updateManager.startUpdateFlowForResult(
                        it,
                        IMMEDIATE,
                        this,
                        REQUEST_CODE_UPDATE)
                }
            }
    }
    fun checkForUpdate(){

        // Creates instance of the manager.
        val appUpdateManager = AppUpdateManagerFactory.create(this)

        // Checks that the platform will allow the specified type of update.
        appUpdateManager.appUpdateInfo.addOnSuccessListener {
            if (it.availableVersionCode() == UpdateAvailability.UPDATE_AVAILABLE &&
            it.isUpdateTypeAllowed(IMMEDIATE)) 
                      {
                    appUpdateManager.startUpdateFlowForResult(
                        it,
                        IMMEDIATE,
                        this,
                        REQUEST_CODE_UPDATE)

                }
            }

    }

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == REQUEST_CODE_UPDATE) {
            if (requestCode != RESULT_OK) {
                Log.e("System out", "Update flow failed! Result code: " + resultCode)
                // If the update is cancelled or fails,
                // you can request to start the update again.
            }
        }
    }
}

我的 Gradle 文件:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'

    implementation 'com.google.android.play:core:1.5.0'
}

当我调试下面的代码时,我在上面的代码,每次我得到如果(appUpdateInfo.availableVersionCode() == 0(UpdateAvailability.UNKNOWN)

日志:

I/PlayCore: UID: [10280]  PID: [22537] AppUpdateService : requestUpdateInfo(com.inappupdate)
I/PlayCore: UID: [10280]  PID: [22537] AppUpdateService : requestUpdateInfo(com.inappupdate)
I/PlayCore: UID: [10280]  PID: [22537] AppUpdateService : Initiate binding to the service.
I/PlayCore: UID: [10280]  PID: [22537] AppUpdateService : Waiting to bind to the service.
I/PlayCore: UID: [10280]  PID: [22537] AppUpdateService : ServiceConnectionImpl.onServiceConnected(ComponentInfo{com.android.vending/com.google.android.finsky.installservice.DevTriggeredUpdateService})
I/PlayCore: UID: [10280]  PID: [22537] AppUpdateService : linkToDeath
2019-05-09 16:11:23.534 22537-23035/com.inappupdate I/PlayCore: UID: [10280]  PID: [22537] OnRequestInstallCallback : onRequestInfo
I/PlayCore: UID: [10280]  PID: [22537] AppUpdateService : Unbind from service.
I/PlayCore: UID: [10280]  PID: [22537] OnRequestInstallCallback : onRequestInfo

所以,如果有人可以指导我如何解决这个错误。

4

1 回答 1

2

在这个字符串中:

(appUpdateInfo.availableVersionCode() == 0(UpdateAvailability.UNKNOWN)

您尝试将 int(app 版本数) 与 UpdateAvailability(0..3) 进行比较

正确的一个是:

(appUpdateInfo.updateAvailability() == 0(UpdateAvailability.UNKNOWN)
于 2019-05-10T07:40:45.460 回答