How get data from cordova-plugin-nativestorage in android java

一笑奈何 提交于 2019-12-01 17:44:23
gillesC

I've made a change, so you should conveniently 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!