我在示例活动中添加了从Block存储中存储和获取数据的功能。当我调用方法来存储数据,然后读取它时,代码在所有设备上都能完美工作。然而,当我卸载应用程序,然后在同一设备上再次安装它,然后尝试读取数据时,它在Android 9,10,11,12上完美工作,但在Android 13上却无法正常工作。我得到一个空的结果列表。
在Pixel 7和Samsung S21上使用Android 13进行的测试
有人知道我需要做什么才能让它在Android 13上工作吗?
尝试步骤:在AndroidManifest.xml中添加allowBackup:true
以下是活动:
class TestBlocksActivity : AppCompatActivity() {
lateinit var binding: ActivityTestBlockBinding
private val blockstoreClient by lazy { Blockstore.getClient(applicationContext) }
private val tokenKey = "key.token.stage"
private val tokenValue = "token.test"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityTestBlockBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.deleteTokenButton.setOnClickListener {
deleteBlockstoreData()
}
binding.storeTokenButton.setOnClickListener {
storeBlockstoreData()
}
binding.loadTokenButton.setOnClickListener {
getBlockstoreData()
}
}
private fun storeBlockstoreData() {
val tokenRequest = StoreBytesData.Builder()
.setShouldBackupToCloud(true)
.setBytes(tokenValue.toByteArray()) // Call this method to set the key value with which the data should be associated with.
.setKey(tokenKey)
.build()
blockstoreClient.storeBytes(tokenRequest)
.addOnSuccessListener {
binding.lastActionText.text = """Token stored: $tokenValue """
binding.storedDataText.text = "Unkown - Load to check"
Timber.tag("BLOCKSTORE_TEST").d("""Stored token: {"$tokenKey", "$tokenValue"}""")
}
.addOnFailureListener { e ->
binding.lastActionText.text = "Exception while storing key"
Timber.tag("BLOCKSTORE_TEST").e(e)
}
}
private fun getBlockstoreData() {
val retrieveRequest = RetrieveBytesRequest.Builder()
.setRetrieveAll(true)
.build()
blockstoreClient.retrieveBytes(retrieveRequest)
.addOnSuccessListener { result ->
if (result.blockstoreDataMap.isEmpty()) {
binding.lastActionText.text = "retrieving token: no token found"
Timber.tag("BLOCKSTORE_TEST").d("Retreiving token: no token found")
}
result.blockstoreDataMap.forEach { (key, value) ->
val valueString = String(value.bytes)
if (key == tokenKey) {
binding.lastActionText.text = "Retrieved token: $valueString"
binding.storedDataText.text = valueString
}
Timber.tag("BLOCKSTORE_TEST").d("""Retrieved token: {"$key", "$valueString"}""")
}
}
.addOnFailureListener {
binding.lastActionText.text = "Exception while retreiving key"
Timber.tag("BLOCKSTORE_TEST").e(it)
}
}
private fun deleteBlockstoreData() {
val deleteBytesRequest = DeleteBytesRequest.Builder()
.setDeleteAll(true)
.build()
blockstoreClient.deleteBytes(deleteBytesRequest)
.addOnSuccessListener { success ->
binding.lastActionText.text = if (success) "Deleted key: $tokenKey"
else "Could not delete key"
if (success) binding.storedDataText.text = "no value"
Timber.tag("BLOCKSTORE_TEST").d("${binding.lastActionText.text}")
}
.addOnFailureListener {
binding.lastActionText.text = "Exception while deleting key"
Timber.tag("BLOCKSTORE_TEST").e(it)
}
}
}
activity_test_block.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp">
<TextView
android:id="@+id/stored_data_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:textSize="20dp"
android:textStyle="bold"
android:text="Loaded token:"/>
<TextView
android:id="@+id/stored_data_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
android:textSize="20dp"
android:text="no token value loaded yet"
app:layout_constraintTop_toBottomOf="@+id/stored_data_label"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<TextView
android:layout_marginTop="20dp"
android:id="@+id/last_action_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/stored_data_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:textSize="20dp"
android:textStyle="bold"
android:text="Last action:"/>
<TextView
android:id="@+id/last_action_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
android:textSize="20dp"
android:text="no token value loaded yet"
app:layout_constraintTop_toBottomOf="@+id/last_action_label"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<com.google.android.material.button.MaterialButton
android:id="@+id/load_token_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/last_action_text"
android:layout_marginTop="40dp"
android:textAllCaps="false"
android:textSize="20dp"
android:text="Load token"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/store_token_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/load_token_button"
android:layout_marginTop="20dp"
android:textAllCaps="false"
android:textSize="20dp"
android:text="Store token"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/delete_token_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/store_token_button"
android:textAllCaps="false"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:text="Delete token"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
1条答案
按热度按时间yruzcnhs1#
我已经解决了。在Android 13上,需要打开Google One备份。