当用户单击关闭按钮时,如何在应用更新立即模式下处理?

时间:2019-05-23 07:37:55

标签: android

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

3 个答案:

答案 0 :(得分:0)

我假设您要禁止用户在应用程序更新之前使用您的应用程序?

为此,您可以听取更新状态,如果用户取消,则可以请求再次开始更新(也许向用户显示对话框/消息,说明为什么需要更新)。

docs

  

获取更新状态的回调开始更新后,可以使用   一个onActivityResult()回调来处理更新失败或   取消,如下所示。

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

答案 1 :(得分:0)

如果用户点击关闭按钮,您可以在这种情况下获得回调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
}

答案 2 :(得分:0)

请试试这个。

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()
            }
        }
    }
}