我可以从java层访问存储在React Native的AsyncStorage中的数据吗?

Sri*_*man 7 java android react-native

我在React Native中使用了少量用户首选项AsyncStorage.现在,我需要从后台服务中获取这些偏好.我无法AsyncStorage从Java层访问.有办法吗?

在iOS中,我们可以导入RCTAsyncLocalStorage.h和调用_getValueForKey.但是,我无法找到在Android中执行此操作的方法.

Sri*_*man 6

我试图使用AsyncStoragejava层.但是,我做不到.所以,我构建了一个React Native插件,用于SharedPreferences从javascript层访问Android的Native .我已经在github上发布了它(https://github.com/sriraman/react-native-shared-preferences)

现在,我使用这个SharedPreferences来自JavaReact Native层.


Muh*_*yaz 5

我有以下代码可以完成这项工作。

import com.facebook.react.modules.storage.AsyncLocalStorageUtil;
import com.facebook.react.modules.storage.ReactDatabaseSupplier;
import android.database.sqlite.SQLiteDatabase;
.....
.....


SQLiteDatabase readableDatabase = null;
readableDatabase = ReactDatabaseSupplier.getInstance(this.getApplicationContext()).getReadableDatabase();
 if (readableDatabase != null) {
    String impl = AsyncLocalStorageUtil.getItemImpl(readableDatabase, "myTableName");
    Log.d(TAG, "impl: " + impl);
  }

====================
and to delete the DB
=====================
SQLiteDatabase database = ReactDatabaseSupplier.getInstance(getContext()).getWritableDatabase();
database.delete(CATALYST_LOCAL_STORAGE, null, null);
Run Code Online (Sandbox Code Playgroud)