Example usage for android.content SharedPreferences getLong

List of usage examples for android.content SharedPreferences getLong

Introduction

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

Prototype

long getLong(String key, long defValue);

Source Link

Document

Retrieve a long value from the preferences.

Usage

From source file:edu.cwru.apo.API.java

private long getUpdateTime(SharedPreferences prefs) {
    return prefs.getLong("updateTime", 0);
}

From source file:org.voidsink.anewjkuapp.mensa.JSONMenuLoader.java

private String getData(Context context) {
    String result = null;//from  w ww .j a  v  a 2s  .c o  m
    String cacheDateKey = PREF_DATE_PREFIX + getCacheKey();
    String cacheDataKey = PREF_DATA_PREFIX + getCacheKey();

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    if (sp.getLong(cacheDateKey, 0) > (System.currentTimeMillis() - 6 * DateUtils.HOUR_IN_MILLIS)) {
        result = sp.getString(cacheDataKey, null);
    }

    if (result == null) {
        try {
            URL url = new URL(getUrl());
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(15000);

            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            Reader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
            result = writer.toString();

            conn.disconnect();

            Editor editor = sp.edit();
            editor.putString(cacheDataKey, result);
            editor.putLong(cacheDateKey, System.currentTimeMillis());
            editor.apply();
        } catch (Exception e) {
            Analytics.sendException(context, e, false);
            result = sp.getString(cacheDataKey, null);
        }
    }

    return result;
}

From source file:org.svij.taskwarriorapp.activities.TasksActivity.java

@Override
protected void onResume() {
    super.onResume();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    long date_long = prefs.getLong("notifications_alarm_time", System.currentTimeMillis());

    Intent myIntent = new Intent(this, NotificationService.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(date_long);

    alarmManager.cancel(pendingIntent);/*from   ww w .j ava 2 s . c om*/
    if (!calendar.getTime().before(new Date())) {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000,
                pendingIntent);
    }
    setTitle(column + " (" + taskListFragment.getListView().getCount() + ")");
}

From source file:net.olejon.spotcommander.MyTools.java

public long getSharedPreferencesLong(final String preference) {
    final SharedPreferences sharedPreferences = mContext.getSharedPreferences("PREFERENCES", 0);
    return sharedPreferences.getLong(preference, 0);
}

From source file:com.sean.takeastand.alarmprocess.ScheduledRepeatingAlarm.java

private void endSessionAnalytics() {
    SharedPreferences sharedPreferences = mContext.getSharedPreferences(Constants.USER_SHARED_PREFERENCES, 0);
    long startTime = sharedPreferences.getLong(Constants.SESSION_START_TIME,
            Calendar.getInstance().getTimeInMillis());
    Calendar currentTime = Calendar.getInstance();
    //Send analytics event only if the user has had to stand at least once in the session
    if (currentTime.getTimeInMillis() - startTime > mCurrentAlarmSchedule.getFrequency()
            * Constants.millisecondsInMinute) {
        long sessionLength = (currentTime.getTimeInMillis() - startTime) / Constants.millisecondsInMinute;
        String analyticsSession = "Session ran for " + Long.toString(sessionLength) + " minutes "
                + "with frequency of " + Integer.toString(mCurrentAlarmSchedule.getFrequency());
        sendAnalyticsEvent(analyticsSession);
    }/*www . ja v a 2 s.co  m*/
}

From source file:net.olejon.mdapp.MyTools.java

public long getSharedPreferencesLong(String preference) {
    SharedPreferences sharedPreferences = mContext.getSharedPreferences("SHARED_PREFERENCES", 0);
    return sharedPreferences.getLong(preference, 0);
}

From source file:com.facebook.android.friendsmash.FriendSmashApplication.java

public void loadInventory() {
    if (ParseUser.getCurrentUser() != null) {
        setBombs(ParseUser.getCurrentUser().getInt("bombs"));
        setCoins(ParseUser.getCurrentUser().getInt("coins"));
    } else {/*from  w w w .j  av a 2 s.  c o m*/
        SharedPreferences prefs = getApplicationContext().getSharedPreferences("Inventory", MODE_PRIVATE);
        long lastSavedTime = prefs.getLong("lastSavedTime", 0);

        if (lastSavedTime == 0) {
            setBombs(NEW_USER_BOMBS);
            setCoins(NEW_USER_COINS);
        } else {
            setBombs(prefs.getInt("bombs", 0));
            setCoins(prefs.getInt("coins", 0));
        }
    }
}

From source file:se.hockersten.timed.vibration.main.CompetitionTab.java

@Override
public void onResume() {
    tapTimes = new LinkedList<Long>();
    SharedPreferences sharedPrefs = getActivity().getPreferences(Context.MODE_PRIVATE);
    for (int i = 0; i < 5; i++) {
        tapTimes.add(i, sharedPrefs.getLong(TAP_TIMES + i, 0));
    }/*from  w w w.j  a v a2s.c om*/

    Button startBtn = (Button) root.findViewById(R.id.mainCompetition_btnStart);
    startBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            flipCompeting();
            updateUI();
        }
    });

    Button tapBtn = (Button) root.findViewById(R.id.mainCompetition_btnTap);
    tapBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            assert (!competing); // this button should be greyed out when not competing, so being able to click it indicates something is wrong
            Calendar currentTime = Calendar.getInstance();

            long timeDiff = currentTime.getTimeInMillis() - lastPress.getTimeInMillis();
            if (tapTimes.size() > 4) {
                tapTimes.removeLast();
            }
            tapTimes.addFirst(timeDiff);
            lastPress = currentTime;

            updateUI();
        }
    });

    if (competing && visible) {
        // take the new wakelock if we are competing
        wakeLock.acquire();
    }
    updateUI();
    super.onResume();
}

From source file:com.example.igorklimov.popularmoviesdemo.sync.SyncAdapter.java

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
        SyncResult syncResult) {/*  w  ww. j av  a 2s.co m*/
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    long lastUpdate = prefs.getLong(mContext.getString(R.string.last_update), System.currentTimeMillis());
    Log.d(LOG_TAG, "onPerformSync: IF " + (currentTimeMillis() - lastUpdate));
    if (System.currentTimeMillis() - lastUpdate >= TWO_DAYS_IN_MILLISECONDS) {
        int delete1 = mContentResolver.delete(MovieByPopularity.CONTENT_URI, null, null);
        int delete2 = mContentResolver.delete(MovieByReleaseDate.CONTENT_URI, null, null);
        int delete3 = mContentResolver.delete(MovieByVotes.CONTENT_URI, null, null);
        Log.d(LOG_TAG, "onPerformSync: ERASE THE DATABASE -------" + delete1);
        Log.d(LOG_TAG, "onPerformSync: ERASE THE DATABASE -------" + delete2);
        Log.d(LOG_TAG, "onPerformSync: ERASE THE DATABASE -------" + delete3);
        prefs.edit().putLong(mContext.getString(R.string.last_update), System.currentTimeMillis()).apply();
        Utility.initializePagePreference(mContext);
        if (MoviesGridFragment.sListener != null)
            MoviesGridFragment.sListener.refresh();
        syncImmediately(mContext);
    } else {
        getData();
    }
}

From source file:com.numenta.taurus.service.TaurusDataSyncService.java

/**
 * {@inheritDoc}/*  w w w .j av  a  2  s  . c  o m*/
 *
 * <p>
 * <b>TAURUS:</b> Only attempt to load metrics once a day to save on network data usage
 * </p>
 *
 * @return Number of new metrics
 */
@Override
protected int loadAllMetrics() throws InterruptedException, ExecutionException, HTMException, IOException {
    // Check last time the metric list was loaded
    Context context = TaurusApplication.getContext();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    long lastSyncTime = prefs.getLong(PREF_LAST_METRIC_SYNC_TIME, 0);
    long now = System.currentTimeMillis();
    // Check if at least 24h has passed since we last downloaded the metrics
    if (now < lastSyncTime + DataUtils.MILLIS_PER_DAY) {
        return 0;
    }

    // Update last synchronization time
    prefs.edit().putLong(PREF_LAST_METRIC_SYNC_TIME, now).apply();
    return super.loadAllMetrics();
}