Example usage for android.content SharedPreferences getString

List of usage examples for android.content SharedPreferences getString

Introduction

In this page you can find the example usage for android.content SharedPreferences getString.

Prototype

@Nullable
String getString(String key, @Nullable String defValue);

Source Link

Document

Retrieve a String value from the preferences.

Usage

From source file:Main.java

public static void loadArray(Context mContext) {
    SharedPreferences mSharedPreference1 = PreferenceManager.getDefaultSharedPreferences(mContext);
    mDishLikeList.clear();/* ww w .jav  a 2  s .  c o  m*/
    int size = mSharedPreference1.getInt("Like_size", 0);

    for (int i = 0; i < size; i++) {
        mDishLikeList.add(mSharedPreference1.getString("Like_" + i, null));
    }
}

From source file:com.apptentive.android.sdk.storage.PersonManager.java

public static CustomData loadCustomPersonData(Context context) {
    SharedPreferences prefs = context.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
    String personDataString = prefs.getString(Constants.PREF_KEY_PERSON_DATA, null);
    try {/*w  w w.  j  av  a  2  s. c om*/
        return new CustomData(personDataString);
    } catch (Exception e) {
        // Ignore
    }
    try {
        return new CustomData();
    } catch (JSONException e) {
        // Ignore
    }
    return null;
}

From source file:com.google.ipc.invalidation.ticl.android.c2dm.C2DMSettings.java

private static String retrieveField(Context context, String field, String defaultValue) {
    SharedPreferences preferences = getPreferences(context);
    return preferences.getString(field, defaultValue);
}

From source file:com.google.android.apps.chrometophone.DeviceRegistrar.java

public static void registerWithServer(final Context context, final String deviceRegistrationID) {
    new Thread(new Runnable() {
        @Override/*from   w  w w  .j  av a2s .co m*/
        public void run() {
            Intent updateUIIntent = new Intent("com.google.ctp.UPDATE_UI");
            try {
                HttpResponse res = makeRequest(context, deviceRegistrationID, REGISTER_PATH);
                if (res.getStatusLine().getStatusCode() == 200) {
                    GCMRegistrar.setRegisteredOnServer(context, true);
                    updateUIIntent.putExtra(STATUS_EXTRA, REGISTERED_STATUS);
                } else if (res.getStatusLine().getStatusCode() == 400) {
                    updateUIIntent.putExtra(STATUS_EXTRA, AUTH_ERROR_STATUS);
                } else {
                    Log.w(TAG, "Registration error " + String.valueOf(res.getStatusLine().getStatusCode()));
                    updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
                }
                context.sendBroadcast(updateUIIntent);
                // Check if this is an update from C2DM to GCM - if it is, remove the
                // old registration id.
                SharedPreferences settings = Prefs.get(context);
                String c2dmRegId = settings.getString("deviceRegistrationID", null);
                if (c2dmRegId != null) {
                    Log.i(TAG, "Removing old C2DM registration id");
                    SharedPreferences.Editor editor = settings.edit();
                    editor.remove("deviceRegistrationID");
                    editor.commit();
                }
            } catch (AppEngineClient.PendingAuthException pae) {
                // Get setup activity to ask permission from user.
                Intent intent = new Intent(SetupActivity.AUTH_PERMISSION_ACTION);
                intent.putExtra("AccountManagerBundle", pae.getAccountManagerBundle());
                context.sendBroadcast(intent);
            } catch (Exception e) {
                Log.w(TAG, "Registration error " + e.getMessage());
                updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
                context.sendBroadcast(updateUIIntent);
            }
        }
    }).start();
}

From source file:foodcenter.android.service.AndroidRequestUtils.java

/**
 * Creates and returns an initialized {@link RequestFactory} of the given
 * type, with authCookie added to the request. <br>
 * RequestUtils.getSharedPrefs will be used.
 *//* ww  w  .ja  v a 2s.  co m*/
private static <T extends RequestFactory> T getRequestFactory(Context context, Class<T> factoryClass) {
    // request factory uses Thread.currentThread().getContextClassLoader() to load factory
    Thread.currentThread().setContextClassLoader(MainActivity.class.getClassLoader());
    T requestFactory = RequestFactorySource.create(factoryClass);

    SharedPreferences prefs = AndroidRequestUtils.getSharedPreferences(context);
    String authCookie = prefs.getString(AndroidRequestUtils.AUTH_COOKIE, null);

    String uriString = AndroidRequestUtils.getBaseUrl() + RF_METHOD;
    URI uri;
    try {
        uri = new URI(uriString);
    } catch (URISyntaxException e) {
        Log.w(TAG, "Bad URI: " + uriString, e);
        return null;
    }
    requestFactory.initialize(new SimpleEventBus(), //
            new AndroidRequestTransport(uri, authCookie, new DefaultHttpClient()));

    return requestFactory;
}

From source file:com.memetro.android.notifications.NotificationUtils.java

/**
 * Gets the current registration id for application on GCM service.
 * <p>// ww  w.  j ava 2 s . c om
 * If result is empty, the registration has failed.
 *
 * @return registration id, or empty string if the registration is not
 * complete.
 */
public static String getRegistrationId(Context context) {
    final SharedPreferences prefs = getGCMPreferences(context);
    String registrationId = prefs.getString(PROPERTY_REG_ID, "");
    if (registrationId.length() == 0) {
        Log.v(TAG, "Registration not found.");
        return "";
    }
    // check if app was updated; if so, it must clear registration id to
    // avoid a race condition if GCM sends a message
    int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
    int currentVersion = getAppVersion(context);
    if (registeredVersion != currentVersion || isRegistrationExpired(context)) {
        Log.v(TAG, "App version changed or registration expired.");
        return "";
    }
    return registrationId;
}

From source file:com.codeskraps.lolo.misc.Utils.java

public static PendingIntent getOnTouchIntent(Context context) {
    PendingIntent pendingIntent = null;/*w  w w . java 2s  .  c  o m*/
    Intent intent = null;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    int onClick = Integer.parseInt(prefs.getString(Constants.ONCLICK, "0"));

    switch (onClick) {
    case 0:
        intent = new Intent("com.codeskraps.lol.DO_NOTHING");
        pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        break;

    case 1:
        intent = new Intent(context, TweetsFeedActivity.class);
        pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        break;

    case 2:
        intent = new Intent();
        intent.setAction(Constants.BROADCAST_RECEIVER);
        pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        break;

    case 3:
        intent = new Intent(context, PrefsActivity.class);
        pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        break;

    case 4:
        intent = new Intent(Intent.ACTION_VIEW);
        String url = prefs.getString(Constants.EURL, context.getString(R.string.prefsURL_default));
        if (!url.startsWith("http://"))
            url = "http://" + url;
        intent.setData(Uri.parse(url));
        pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        break;
    }
    return pendingIntent;
}

From source file:com.halseyburgund.rwframework.util.RWSharedPrefsHelper.java

/**
 * Retrieves the data for the specified key from the shared preferences
 * and tries to create a JSON Array from it, which string representation
 * will be returned//from   w  ww.  j av a  2  s . c  om
 * 
 * @param context to be used to access shared preferences
 * @param preferencesName of shared preferences to be used
 * @param key for retrieving the data
 * @return string representation of the JSON array data retrieved
 */
public static String loadJSONArray(Context context, String preferencesName, String key) {
    try {
        SharedPreferences settings = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
        JSONArray array = new JSONArray(settings.getString(RWSharedPrefsHelper.PREFIX + key, "[]"));
        return array.toString();
    } catch (JSONException e) {
        Log.e(TAG, RWConfiguration.JSON_SYNTAX_ERROR_MESSAGE, e);
        return null;
    }
}

From source file:com.ilearnrw.reader.utils.HttpHelper.java

public static boolean log(Context ctx, String msg, String tag) {
    if (msg.trim().isEmpty())
        return false;

    if (!isNetworkAvailable(ctx))
        return false;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    String username = prefs.getString(ctx.getString(R.string.sp_user_name), "");

    if (username.isEmpty())
        return false;

    new LogTask(tag).run(username, msg);

    return true;/*  w w w  .  j  a  va2  s .  c  o  m*/
}

From source file:com.halseyburgund.rwframework.util.RWSharedPrefsHelper.java

/**
 * Retrieves the data for the specified key from the shared preferences
 * and tries to create a JSON Object from it, which string representation
 * will be returned/*from www. j a  v  a  2s . c o  m*/
 * 
 * @param context to be used to access shared preferences
 * @param preferencesName of shared preferences to be used
 * @param key for retrieving the data
 * @return string representation of the JSON object data retrieved
 */
public static String loadJSONObject(Context context, String preferencesName, String key) {
    try {
        SharedPreferences settings = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
        JSONObject object = new JSONObject(settings.getString(RWSharedPrefsHelper.PREFIX + key, "{}"));
        return object.toString();
    } catch (JSONException e) {
        Log.e(TAG, RWConfiguration.JSON_SYNTAX_ERROR_MESSAGE, e);
        return null;
    }
}