سؤال

I have several apps that give the best user experience when my server knows that it's the same user across apps. To verify who the user is, each user has a user_token string that is sent to my server when the app is loaded.

I'm trying to figure out the best way to share this string across apps. It seems like Content Provider is the best way to share data between apps, but a SQLite database seems a little overkill for storing/retrieving a string.

I asked this same question on an Android-Dev IRC and it was recommended that I use SharedPreferences for this - but in looking through some other SO posts on this, that's not recommended to do.

What would be the best way to do this? If it's the content provider, any idea what kind of data structure I should use for storing the string?

Thanks in advance!

هل كانت مفيدة؟

المحلول

If your apps are not sharing a process then a SharedPreference is not a good idea. It is not multi-process safe. ContentProviders were constructed for this purpose: sharing data between apps. You can create your own ContentProvider which stores the data with a SharedPreference, you don't have to use SQLite. The ContentProvider APIs are well suited for SQL type implementations, but it is not a requirement.

نصائح أخرى

Here's what my onCreate method in the content provider looks like (which exposes the sharedprefs to the two apps):

@Override
    public synchronized boolean onCreate() {
        // TODO Auto-generated method stub
        SharedPreferences sp = getContext().getSharedPreferences(
                "SurveyMeSharedPreferences", getContext().MODE_PRIVATE);
        String apiKey = sp.getString("user_auth_token", null);

        if (apiKey == null) {
            SharedPreferences.Editor editor = sp.edit();
            try {
                apiKey = SurveyMe.checkUser();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            editor.putString("user_auth_token", apiKey);
            editor.commit();
        }

        return false;
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top