如何从androidova中的cordova-plugin-nativestorage获取数据 [英] How get data from cordova-plugin-nativestorage in android java
问题描述
Good day,
我为Cordova做原生背景mod,我需要从js到java的数据。
我使用插件 cordova-plugin-nativestorage 保存数据,使用此代码:
<!DOCTYPE html>
< html>
< head>
< title>保存数据< / title>
< script type =text / javascriptcharset =utf-8src =cordova.js>< / script&
< script type =text / javascriptcharset =utf-8>
function onLoad(){
document.addEventListener(deviceready,onDeviceReady,false);
}
function onDeviceReady(){
NativeStorage.setItem(somekey,value,null,null);
}
< / script>
< / head>
< body onload =onLoad()>
< / body>
< / html>
但我的问题是:我不知道如何获取这个数据java在没有Cordova活动的后台服务中。
Backgroud服务java:
包cz.oznameni;
public class Backgroundoznameni extends Service {
}
请重新安装插件:
/ p> cordova插件删除cordova-plugin-nativestorage
cordova插件添加https://github.com/TheCocoaProject/cordova- plugin-nativestorage
这将安装dev版本。这是因为此更新的代码不会推送到NPM( UPDATE :现在没有必要使用开发版本,NPM上的版本很好)。
为了使用键检索值,我写了以下方法:
String getValue context,String key,String defaultValue){
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME,Activity.MODE_PRIVATE);
return settings.getString(key,defaultValue);
}
PREFS_NAME
如下声明:
public static final String PREFS_NAME =NativeStorage;
上下文应该是 this
onCreate
方法。
总体来说应该是这样:
public class Backgroundoznameni extends Service {
public static final String PREFS_NAME =NativeStorage;
@Override
public void onCreate(){
String value = getValue(Backgroundoznameni.this,somekey,null);
}
String getValue(Context context,String key,String defaultValue){
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME,Activity.MODE_PRIVATE);
return settings.getString(key,defaultValue);
}
}
注意:代码未测试!
编辑:这进一步记录在此Github问题。
Good day, I making native background mod for Cordova and I need get data from js to java. I save data in js with plugin cordova-plugin-nativestorage, with this code:
<!DOCTYPE html>
<html>
<head>
<title>Save data</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
NativeStorage.setItem("somekey", "value", null, null);
}
</script>
</head>
<body onload="onLoad()">
</body>
</html>
but my problem is: I don’t know how to get this data ("value") with java in background services without Cordova activity.
Backgroud services java:
package cz.oznameni;
public class Backgroundoznameni extends Service {
}
I've made a change, so you chould conviently access the saved value.
Please first reinstall the plugin:
cordova plugin remove cordova-plugin-nativestorage
cordova plugin add https://github.com/TheCocoaProject/cordova-plugin-nativestorage
This will install the dev version. This because this updated code isn't pushed to NPM (UPDATE: it is now not necessary to use the dev version, the version on NPM is just fine).
For retrieving the value with a key , I've written the following method:
String getValue(Context context, String key, String defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
return settings.getString(key, defaultValue);
}
The PREFS_NAME
should be declared as follows:
public static final String PREFS_NAME = "NativeStorage";
The context should be this
accessable within the onCreate
method.
So overall it should look something like this:
public class Backgroundoznameni extends Service {
public static final String PREFS_NAME = "NativeStorage";
@Override
public void onCreate() {
String value = getValue(Backgroundoznameni.this, "somekey", null);
}
String getValue(Context context, String key, String defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
return settings.getString(key, defaultValue);
}
}
NOTE: Code not tested!
EDIT: This is further documented in this Github issue.
这篇关于如何从androidova中的cordova-plugin-nativestorage获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!