阻止存储在Android 13上不存储数据

2q5ifsrm  于 2023-06-20  发布在  Android
关注(0)|答案(1)|浏览(172)

我在示例活动中添加了从Block存储中存储和获取数据的功能。当我调用方法来存储数据,然后读取它时,代码在所有设备上都能完美工作。然而,当我卸载应用程序,然后在同一设备上再次安装它,然后尝试读取数据时,它在Android 9,10,11,12上完美工作,但在Android 13上却无法正常工作。我得到一个空的结果列表。
在Pixel 7和Samsung S21上使用Android 13进行的测试
有人知道我需要做什么才能让它在Android 13上工作吗?
尝试步骤:在AndroidManifest.xml中添加allowBackup:true
以下是活动:

  1. class TestBlocksActivity : AppCompatActivity() {
  2. lateinit var binding: ActivityTestBlockBinding
  3. private val blockstoreClient by lazy { Blockstore.getClient(applicationContext) }
  4. private val tokenKey = "key.token.stage"
  5. private val tokenValue = "token.test"
  6. override fun onCreate(savedInstanceState: Bundle?) {
  7. super.onCreate(savedInstanceState)
  8. binding = ActivityTestBlockBinding.inflate(layoutInflater)
  9. setContentView(binding.root)
  10. binding.deleteTokenButton.setOnClickListener {
  11. deleteBlockstoreData()
  12. }
  13. binding.storeTokenButton.setOnClickListener {
  14. storeBlockstoreData()
  15. }
  16. binding.loadTokenButton.setOnClickListener {
  17. getBlockstoreData()
  18. }
  19. }
  20. private fun storeBlockstoreData() {
  21. val tokenRequest = StoreBytesData.Builder()
  22. .setShouldBackupToCloud(true)
  23. .setBytes(tokenValue.toByteArray()) // Call this method to set the key value with which the data should be associated with.
  24. .setKey(tokenKey)
  25. .build()
  26. blockstoreClient.storeBytes(tokenRequest)
  27. .addOnSuccessListener {
  28. binding.lastActionText.text = """Token stored: $tokenValue """
  29. binding.storedDataText.text = "Unkown - Load to check"
  30. Timber.tag("BLOCKSTORE_TEST").d("""Stored token: {"$tokenKey", "$tokenValue"}""")
  31. }
  32. .addOnFailureListener { e ->
  33. binding.lastActionText.text = "Exception while storing key"
  34. Timber.tag("BLOCKSTORE_TEST").e(e)
  35. }
  36. }
  37. private fun getBlockstoreData() {
  38. val retrieveRequest = RetrieveBytesRequest.Builder()
  39. .setRetrieveAll(true)
  40. .build()
  41. blockstoreClient.retrieveBytes(retrieveRequest)
  42. .addOnSuccessListener { result ->
  43. if (result.blockstoreDataMap.isEmpty()) {
  44. binding.lastActionText.text = "retrieving token: no token found"
  45. Timber.tag("BLOCKSTORE_TEST").d("Retreiving token: no token found")
  46. }
  47. result.blockstoreDataMap.forEach { (key, value) ->
  48. val valueString = String(value.bytes)
  49. if (key == tokenKey) {
  50. binding.lastActionText.text = "Retrieved token: $valueString"
  51. binding.storedDataText.text = valueString
  52. }
  53. Timber.tag("BLOCKSTORE_TEST").d("""Retrieved token: {"$key", "$valueString"}""")
  54. }
  55. }
  56. .addOnFailureListener {
  57. binding.lastActionText.text = "Exception while retreiving key"
  58. Timber.tag("BLOCKSTORE_TEST").e(it)
  59. }
  60. }
  61. private fun deleteBlockstoreData() {
  62. val deleteBytesRequest = DeleteBytesRequest.Builder()
  63. .setDeleteAll(true)
  64. .build()
  65. blockstoreClient.deleteBytes(deleteBytesRequest)
  66. .addOnSuccessListener { success ->
  67. binding.lastActionText.text = if (success) "Deleted key: $tokenKey"
  68. else "Could not delete key"
  69. if (success) binding.storedDataText.text = "no value"
  70. Timber.tag("BLOCKSTORE_TEST").d("${binding.lastActionText.text}")
  71. }
  72. .addOnFailureListener {
  73. binding.lastActionText.text = "Exception while deleting key"
  74. Timber.tag("BLOCKSTORE_TEST").e(it)
  75. }
  76. }
  77. }

activity_test_block.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto">
  4. <androidx.constraintlayout.widget.ConstraintLayout
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:padding="24dp">
  8. <TextView
  9. android:id="@+id/stored_data_label"
  10. android:layout_width="0dp"
  11. android:layout_height="wrap_content"
  12. app:layout_constraintTop_toTopOf="parent"
  13. app:layout_constraintStart_toStartOf="parent"
  14. app:layout_constraintEnd_toEndOf="parent"
  15. android:textSize="20dp"
  16. android:textStyle="bold"
  17. android:text="Loaded token:"/>
  18. <TextView
  19. android:id="@+id/stored_data_text"
  20. android:layout_width="0dp"
  21. android:layout_height="wrap_content"
  22. android:gravity="start"
  23. android:textSize="20dp"
  24. android:text="no token value loaded yet"
  25. app:layout_constraintTop_toBottomOf="@+id/stored_data_label"
  26. app:layout_constraintStart_toStartOf="parent"
  27. app:layout_constraintEnd_toEndOf="parent"
  28. />
  29. <TextView
  30. android:layout_marginTop="20dp"
  31. android:id="@+id/last_action_label"
  32. android:layout_width="0dp"
  33. android:layout_height="wrap_content"
  34. app:layout_constraintTop_toBottomOf="@+id/stored_data_text"
  35. app:layout_constraintStart_toStartOf="parent"
  36. app:layout_constraintEnd_toEndOf="parent"
  37. android:textSize="20dp"
  38. android:textStyle="bold"
  39. android:text="Last action:"/>
  40. <TextView
  41. android:id="@+id/last_action_text"
  42. android:layout_width="0dp"
  43. android:layout_height="wrap_content"
  44. android:gravity="start"
  45. android:textSize="20dp"
  46. android:text="no token value loaded yet"
  47. app:layout_constraintTop_toBottomOf="@+id/last_action_label"
  48. app:layout_constraintStart_toStartOf="parent"
  49. app:layout_constraintEnd_toEndOf="parent"
  50. />
  51. <com.google.android.material.button.MaterialButton
  52. android:id="@+id/load_token_button"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. app:layout_constraintStart_toStartOf="parent"
  56. app:layout_constraintEnd_toEndOf="parent"
  57. app:layout_constraintTop_toBottomOf="@+id/last_action_text"
  58. android:layout_marginTop="40dp"
  59. android:textAllCaps="false"
  60. android:textSize="20dp"
  61. android:text="Load token"/>
  62. <com.google.android.material.button.MaterialButton
  63. android:id="@+id/store_token_button"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. app:layout_constraintStart_toStartOf="parent"
  67. app:layout_constraintEnd_toEndOf="parent"
  68. app:layout_constraintTop_toBottomOf="@+id/load_token_button"
  69. android:layout_marginTop="20dp"
  70. android:textAllCaps="false"
  71. android:textSize="20dp"
  72. android:text="Store token"/>
  73. <com.google.android.material.button.MaterialButton
  74. android:id="@+id/delete_token_button"
  75. android:layout_width="wrap_content"
  76. android:layout_height="wrap_content"
  77. app:layout_constraintStart_toStartOf="parent"
  78. app:layout_constraintEnd_toEndOf="parent"
  79. app:layout_constraintTop_toBottomOf="@+id/store_token_button"
  80. android:textAllCaps="false"
  81. android:layout_marginTop="20dp"
  82. android:textSize="20dp"
  83. android:text="Delete token"/>
  84. </androidx.constraintlayout.widget.ConstraintLayout>
  85. </layout>
yruzcnhs

yruzcnhs1#

我已经解决了。在Android 13上,需要打开Google One备份。

相关问题