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 should conveniently access the saved value.
Please, first reinstall the plugin:
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:
The
PREFS_NAME
should be declared as follows:The context should be
this
accessable within theonCreate
method.So overall it should look something like this:
NOTE: Code not tested!
EDIT: This is further documented in this Github issue.