Example usage for android.app Activity getSharedPreferences

List of usage examples for android.app Activity getSharedPreferences

Introduction

In this page you can find the example usage for android.app Activity getSharedPreferences.

Prototype

@Override
    public SharedPreferences getSharedPreferences(String name, int mode) 

Source Link

Usage

From source file:Main.java

private static void writeCache(int[] screen, Activity activity) {

    SharedPreferences config = activity.getSharedPreferences("config", Context.MODE_PRIVATE);
    Editor editor = config.edit();// ww  w  .jav a  2 s.com

    editor.putBoolean("isAlreadyGetPhoneResolution", true);
    editor.putInt("height", screen[0]);
    editor.putInt("width", screen[1]);
    editor.commit();
}

From source file:Main.java

public static void setStoredVar(Activity activity, String key, String val) {
    SharedPreferences settings = activity.getSharedPreferences(key, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString(key, val);
    editor.commit();/*from w w  w . j  a va2  s.  com*/
}

From source file:Main.java

public static void setSessionField(Activity activity, String field, String value) {
    SharedPreferences sharedPref = activity.getSharedPreferences("Session", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(field, value);//from   ww w  .j  av a  2  s  .c om
    editor.commit();
}

From source file:Main.java

public static String getStoredVar(Activity activity, String key) {
    SharedPreferences settings = activity.getSharedPreferences(key, Context.MODE_PRIVATE);
    return settings.getString(key, "");
}

From source file:hu.balazsbakai.sq.util.UsedServersUtil.java

private static String readUsedServerData(Activity activity) {
    SharedPreferences settings = activity.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    String data = settings.getString(USED_SERVERS, "");
    LogUtil.i("UsedServersUtil.readUsedServerData", data);
    return data;/* w  w  w. j a  v  a 2  s .c  om*/
}

From source file:hu.balazsbakai.sq.util.UsedServersUtil.java

private static void writeUsedServerData(Activity activity, String data) {
    LogUtil.i("UsedServersUtil.writeUsedServerData", data);

    SharedPreferences settings = activity.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString(USED_SERVERS, data);
    editor.commit();//from   ww w.j a v  a2 s.  c o m
}

From source file:de.Maxr1998.xposed.maxlock.ui.LockActivity.java

@SuppressLint("WorldReadableFiles")
public static void directUnlock(Activity caller, Intent orig, String pkgName) {
    try {/*from w w w .ja v a 2  s.  c  om*/
        //noinspection deprecation
        caller.getSharedPreferences(Common.PREFS_PACKAGES, MODE_WORLD_READABLE).edit()
                .putLong(pkgName + "_tmp", System.currentTimeMillis()).commit();
        orig.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION
                | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        caller.startActivity(orig);
    } catch (Exception e) {
        Intent intent_option = caller.getPackageManager().getLaunchIntentForPackage(pkgName);
        intent_option.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION
                | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        caller.startActivity(intent_option);
    } finally {
        caller.finish();
    }
}

From source file:br.fucapi.fapeam.monitori.eula.Eula.java

/**
 * Displays the EULA if necessary. This method should be called from the
 * onCreate() method of your main Activity.
 * // w ww  .  j  a  v  a  2 s  .c om
 * @param activity
 *            The Activity to finish if the user rejects the EULA.
 * @return Whether the user has agreed already.
 */
public static boolean show(final Activity activity, int title, int acceptLabel, int refuseLabel) {
    final SharedPreferences preferences = activity.getSharedPreferences(PREFERENCES_EULA,
            Activity.MODE_PRIVATE);
    if (!preferences.getBoolean(PREFERENCE_EULA_ACCEPTED, false)) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(title);
        builder.setCancelable(true);
        builder.setPositiveButton(acceptLabel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                accept(preferences);
                if (activity instanceof OnEulaAgreedTo) {
                    ((OnEulaAgreedTo) activity).onEulaAgreedTo();
                }
            }
        });
        builder.setNegativeButton(refuseLabel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                refuse(activity);
            }
        });
        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
                refuse(activity);
            }
        });
        builder.setMessage(readEula(activity));
        builder.create().show();
        return false;
    }
    return true;
}

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  w  w .  j a  v a2s .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: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;
}