Sri*_*man 7 java android react-native
我在React Native中使用了少量用户首选项AsyncStorage
.现在,我需要从后台服务中获取这些偏好.我无法AsyncStorage
从Java层访问.有办法吗?
在iOS中,我们可以导入RCTAsyncLocalStorage.h
和调用_getValueForKey
.但是,我无法找到在Android中执行此操作的方法.
我试图使用AsyncStorage
java层.但是,我做不到.所以,我构建了一个React Native插件,用于SharedPreferences
从javascript层访问Android的Native .我已经在github上发布了它(https://github.com/sriraman/react-native-shared-preferences)
现在,我使用这个SharedPreferences
来自Java
和React Native
层.
我有以下代码可以完成这项工作。
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)