Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Gideros app reading Android SharedPreferences? — Gideros Forum

Gideros app reading Android SharedPreferences?

PaulHPaulH Member
edited March 2017 in General questions
Hey, folks. I'm preparing to release a Gideros port of an app previously released coded in native Java for Android. Some users of the old version may have a lot of data, stored in SharedPreferences in the usual way for a Java Android app. With the new Gideros version, I'm storing user data in files in the documents folder, the usual way for a Gideros app. I need a way to move stored data from the former to the latter, as a one-time step, so existing users don't lose their data when they install this update. So I'm looking into how to either:

A- Drop some native Java into the exported Eclipse project for Android to read the SharedPreferences and write the data into the Gideros documents folder for the app

or

B- Get the Lua code in the Gideros app to read from the SharedPreferences directly

I'm guessing that B would require writing a plugin, so A seems like the simplest option.

Does anyone have any experience reading from SharedPreferences via Lua, or writing to the Gideros document folder from native Java? Do you have any helpful insights on doing either?

Paul

Background - The app in question is a fishing guide called My Fishing Advisor. The old version of the app is also on iOS, but that version is so out of date that it doesn't store enough data to be worth trying to move it over. The Android version has had updates that allow users to store details about their fishing spots, and that's the data I want to preserve. The app has a feature to backup data to the cloud, but using that feature is optional. If I switch to a different method of storing the data without importing from the old to the new, many users would lose a lot of data that's important to them. One of the main reasons I've ported the app to Lua and Gideros is to have a common code base so I can keep the iOS version caught up with the Android version in the future.


Comments

  • PaulHPaulH Member
    I seem to have found what I was looking for. In case anyone else finds themselves in this position (porting a previously published Android app to Lua with Gideros and wanting to preserve saved data when people install the new version) this looks like it will work:

    Drop code like this into the onCreate method in the YourAppNameActivity.java file in the exported Android project:

    try {
    SharedPreferences sp = getSharedPreferences(YOUR_PREFS_ID, 0);
    // Retrieve saved data from SharedPreferences the way the old app did.
    byte saved_data[] = ... // Build saved_data, stream of bytes containing saved data
    FileOutputStream f = new FileOutputStream(new File("data/data/YOUR_APP_PACKAGE/files", SOME_FILE_NAME));
    f.write(saved_data);
    f.close();
    }
    ...

    Then in lua, just open "|D|SOME_FILE_NAME" to read the data to import it and save it however you like.

    In my case I'll do that only if the files I currently use to store user data aren't already present, so it should be a one-time conversion from the old storage method to the new, invisible to the user.

    I don't know if anyone else it likely to need to do that, but just in case I thought I'd share this.

    Paul


    +1 -1 (+5 / -0 )Share on Facebook
Sign In or Register to comment.