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

/**
 * Returns the cached device group key for this device. Device secret is generated when the device
 * is confirmed and is used for device identification.
 *
 * @param username          REQUIRED: The current user.
 * @param userPoolId        REQUIRED: Client ID of the application.
 * @param context           REQUIRED: Application context.
 * @return device group key as String, null if the device-key is not available.
 *//*w ww . j  a  v  a2 s  .c o m*/
public static String getDeviceGroupKey(String username, String userPoolId, Context context) {
    try {
        SharedPreferences cipCachedDeviceDetails = context
                .getSharedPreferences(getDeviceDetailsCacheForUser(username, userPoolId), 0);
        if (cipCachedDeviceDetails != null && cipCachedDeviceDetails.contains(COGNITO_DEVICE_GROUP_KEY)) {
            return cipCachedDeviceDetails.getString(COGNITO_DEVICE_GROUP_KEY, null);
        }
    } catch (Exception e) {
        Log.e(TAG, "Error accessing SharedPreferences" + e.getMessage());
    }
    return null;
}

From source file:realizer.com.schoolgenieparent.ServerUtilities.java

private static StringBuilder post(String regID, String EmpId) {
    //String my = "http://104.217.254.180/RestWCF/svcEmp.svc/registerDevice/" + EmpId + "/" + regID;
    SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(ctx);
    String did = sharedpreferences.getString("DWEVICEID", "");

    //String my = "http://192.168.1.14/SJRestWCF/registerDevice/" + EmpId + "/" + regID;
    String my = Config.URL + "RegisterStudentDevice/" + EmpId + "/" + did + "/" + regID;
    Log.d("GCMDID", my);
    builder = new StringBuilder();
    HttpGet httpGet = new HttpGet(my);
    HttpClient client = new DefaultHttpClient();
    try {//from w w  w  .  ja  v a 2 s  .  c o  m
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();

        int statusCode = statusLine.getStatusCode();
        Log.d("GCMSTATUS", "" + statusCode);
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e("Error", "Failed to Login");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        client.getConnectionManager().closeExpiredConnections();
        client.getConnectionManager().shutdown();
    }

    return builder;
}

From source file:com.jdom.ajatt.viewer.util.HtmlUtil.java

public static String getRequest(Activity activity, final String url) {
    SharedPreferences prefs = activity.getSharedPreferences(CLASS_NAME, Context.MODE_PRIVATE);
    String cachedUrlContents = prefs.getString(url, null);
    String urlRetrievalTimeKey = url + ".time";
    long cachedUrlRetrievalTime = prefs.getLong(urlRetrievalTimeKey, 0L);
    long ageOfCachedData = System.currentTimeMillis() - cachedUrlRetrievalTime;
    if (cachedUrlRetrievalTime == 0) {
        Log.d(CLASS_NAME, "Did not find cached data for URL [" + url + "].");
    } else {// w  ww. j a v a 2  s  . c o m
        Log.d(CLASS_NAME, "URL [" + url + "] has been cached for [" + ageOfCachedData + "] ms.");
    }

    Future<String> result = null;

    boolean expired = ageOfCachedData > CACHE_URL_MILLISECONDS;

    if (expired) {
        Log.d(CLASS_NAME, "URL [" + url + "] data is stale.");
    } else {
        long timeRemainingValidCache = CACHE_URL_MILLISECONDS - ageOfCachedData;
        Log.d(CLASS_NAME,
                "URL [" + url + "] data has [" + timeRemainingValidCache + "] ms of validity remaining.");
    }

    if (cachedUrlContents == null || expired) {
        Callable<String> callable = new Callable<String>() {
            public String call() throws Exception {
                long start = System.currentTimeMillis();
                Log.d(CLASS_NAME, "Retrieving URL [" + url + "].");
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet(url);
                try {
                    HttpResponse response = client.execute(request);
                    return HttpHelper.request(response);
                } catch (Exception ex) {
                    Log.e(CLASS_NAME, "Failure to retrieve the url!", ex);
                    return null;
                } finally {
                    Log.d(CLASS_NAME, "Retrieving URL [" + url + "] took ["
                            + (System.currentTimeMillis() - start) + "] ms to retrieve.");
                }
            }
        };

        ExecutorService executor = Executors.newSingleThreadExecutor();
        result = executor.submit(callable);
    }

    if (cachedUrlContents == null) {
        try {
            cachedUrlContents = result.get();

            Editor editor = prefs.edit();
            editor.putLong(urlRetrievalTimeKey, System.currentTimeMillis());
            editor.putString(url, cachedUrlContents);
            editor.commit();
        } catch (Exception e) {
            Log.e(CLASS_NAME, "Failure to retrieve the url!", e);
        }
    }

    return cachedUrlContents;
}

From source file:com.saulcintero.moveon.osm.OSMHelper.java

public static OAuthConsumer GetOSMAuthConsumer(Context ctx) {

    OAuthConsumer consumer = null;/*w  w w.  j  a v a2  s.  c  o m*/

    try {
        int osmConsumerKey = ctx.getResources().getIdentifier("osm_consumerkey", "string",
                ctx.getPackageName());
        int osmConsumerSecret = ctx.getResources().getIdentifier("osm_consumersecret", "string",
                ctx.getPackageName());
        consumer = new CommonsHttpOAuthConsumer(ctx.getString(osmConsumerKey),
                ctx.getString(osmConsumerSecret));

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        String osmAccessToken = prefs.getString("osm_accesstoken", "");
        String osmAccessTokenSecret = prefs.getString("osm_accesstokensecret", "");

        if (osmAccessToken != null && osmAccessToken.length() > 0 && osmAccessTokenSecret != null
                && osmAccessTokenSecret.length() > 0) {
            consumer.setTokenWithSecret(osmAccessToken, osmAccessTokenSecret);
        }
    } catch (Exception e) {
    }

    return consumer;
}

From source file:com.scoreflex.ScoreflexGcmClient.java

protected static String getRegistrationId(Context c) {
    final SharedPreferences prefs = Scoreflex.getSharedPreferences(c);
    String registrationId = prefs.getString(GCM_REGISTRATION_ID_PREF_NAME, "");
    if (registrationId == null || registrationId.length() > 0) {
        return "";
    }//from www.j a v a 2 s. co  m

    int registeredVersion = prefs.getInt(GCM_REGISTRATION_APP_VERSION_PREF_NAME, Integer.MIN_VALUE);
    int currentVersion = getAppVersion(Scoreflex.getApplicationContext());
    if (registeredVersion != currentVersion) {
        return "";
    }
    return registrationId;
}

From source file:pt.hive.cameo.ProxyRequest.java

public static boolean isReady(Activity activity) {
    SharedPreferences preferences = activity.getSharedPreferences("cameo", Context.MODE_PRIVATE);
    String baseUrl = preferences.getString("baseUrl", null);
    String sessionId = preferences.getString("sessionId", null);
    return baseUrl != null && sessionId != null;
}

From source file:com.dajodi.scandic.Util.java

/**
 * Reads the username/password from.//from  ww  w . ja  va2 s  .c  o  m
 * 
 * @param context
 * @return
 */
public static UsernamePassword read(Context context) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    String username = prefs.getString(USERNAME_KEY, null);
    String password = prefs.getString(PASSWORD_KEY, null);

    if (username == null || password == null || (password.length() == 0)) {
        return null;
    }

    return new UsernamePassword(username, password);
}

From source file:minghai.nisesakura.SimpleTwitterHelper.java

static String loadRefreshURL(Context context) {
    SharedPreferences prefs = context.getSharedPreferences(NiseSakuraWidgetUpdateService.PREFS_NAME, 0);
    String refreshURL = prefs.getString(REFRESH_URL_KEY, QUERY_OPTION);
    return refreshURL;
}

From source file:io.lqd.sdk.model.LQDevice.java

public synchronized static String getDeviceID(Context context) {
    String uid;//from  w  w w  . j  ava 2 s  .c o m
    SharedPreferences sharedPrefs = context.getSharedPreferences("io.lqd.UUID", Context.MODE_PRIVATE);
    uid = sharedPrefs.getString("io.lqd.UUID", null);
    if (uid == null) {
        uid = LQModel.newIdentifier();
        Editor editor = sharedPrefs.edit();
        editor.putString("io.lqd.UUID", uid);
        editor.commit();
    }
    return uid;
}

From source file:com.umeng.comm.ui.utils.FeedMemento.java

/**
 * @param context/*from   w w  w  . ja  v a  2 s. c  o  m*/
 * @return
 */
public static FeedItem restoreMemento(Context context) {
    FeedItem feedItem = new FeedItem();
    SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.FEED_SHARE_PREF,
            Context.MODE_PRIVATE);
    String saveFeedStr = sharedPreferences.getString(Constants.FEED_SHARE_PREF_KEY, "");
    if (!TextUtils.isEmpty(saveFeedStr)) {
        try {
            JSONObject feedJsonObject = new JSONObject(saveFeedStr);
            feedItem.text = feedJsonObject.optString("content");
            feedItem.locationAddr = feedJsonObject.optString("location_addr");
            // 
            parseImages(feedItem, feedJsonObject);
            // ?
            parseTopics(feedItem, feedJsonObject);
            // ?
            parseFriends(feedItem, feedJsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } // end if

    return feedItem;
}