android 如何处理在应用程序更新即时模式下,当用户点击关闭按钮?

nlejzf6q  于 2022-11-27  发布在  Android
关注(0)|答案(3)|浏览(139)

根据文档,对于应用程序更新API实现,我使用的是即时模式。但全屏UI上有关闭按钮。当用户单击关闭按钮时,UI被关闭。在这种情况下,如何处理此即时流,因为我不希望用户使用我的应用程序?

yeotifhr

yeotifhr1#

请试试这个。

override fun onResume() {
    super.onResume()
    if (listener != null) {
        appUpdateManager.registerListener(listener)
    }
    appUpdateManager
        .appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
            ) {
                appUpdateManager.startUpdateFlowForResult(
                    appUpdateInfo,
                    AppUpdateType.IMMEDIATE,
                    this,
                    100
                )
            } else if (appUpdateInfo.updateAvailability()
                == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS
            ) {
                appUpdateManager.startUpdateFlowForResult(
                    appUpdateInfo,
                    IMMEDIATE,
                    this,
                    100
                );
            }
        }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    // super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 100) {
        when (resultCode) {
            Activity.RESULT_OK -> {
                Toast.makeText(this, "RESULT_OK", Toast.LENGTH_LONG).show()
            }
            Activity.RESULT_CANCELED -> {
                Toast.makeText(this, "RESULT_CANCELED", Toast.LENGTH_LONG).show()
                finish()
            }
            ActivityResult.RESULT_IN_APP_UPDATE_FAILED -> {
                Toast.makeText(this, "RESULT_IN_APP_UPDATE_FAILED", Toast.LENGTH_LONG).show()
                finish()
            }
            else -> {
                Toast.makeText(this, "No result", Toast.LENGTH_LONG).show()
            }
        }
    }
}
ppcbkaq5

ppcbkaq52#

如果用户点击关闭按钮,您可以在此条件下获得回调resultCode == Activity.RESULT_CANCELED:-

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
    // React to activity result and if request code == ResultActivity.REQUEST_CODE
    if (requestCode == UPDATE_REQUEST_CODE) {
        //when user clicks update button
        if (resultCode == Activity.RESULT_OK) {
            Toast.makeText(mContext, "App download starts...", Toast.LENGTH_LONG).show()
        } else if (resultCode == Activity.RESULT_CANCELED) {
            //if user click on close button
            Toast.makeText(mContext, "App download canceled.", Toast.LENGTH_LONG).show()
           //Here you can do whatever you want (call finish() to close the app.)
        } else if (resultCode == ActivityResult.RESULT_IN_APP_UPDATE_FAILED) {
            Toast.makeText(mContext, "App download failed.", Toast.LENGTH_LONG).show()
        }
        return true
    }
    return false
}
mutmk8jj

mutmk8jj3#

我想你想不允许用户使用你的应用程序,直到他们更新应用程序?
为此,您可以监听更新状态,如果用户取消,您可以请求再次启动更新(可能会向用户显示一个对话框/消息,解释为什么需要更新)。
从文档中

获取更新状态回调

启动更新后,可以使用onActivityResult()回调来处理更新失败或取消,如下所示。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == MY_REQUEST_CODE) {
    if (resultCode != RESULT_OK) {
      log("Update flow failed! Result code: " + resultCode);
      // If the update is cancelled or fails,
      // you can request to start the update again.
    }
  }
}

相关问题