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:org.chromium.chrome.browser.download.DownloadManagerDelegate.java

/**
 * Removes a download Id mapping from the SharedPreferences given the download GUID.
 * @param guid Download GUID.//  w w  w . j a va 2  s  .  c  om
 * @return the Android DownloadManager's download ID that is removed, or
 *         INVALID_SYSTEM_DOWNLOAD_ID if it is not found.
 */
private long removeDownloadIdMapping(String downloadGuid) {
    SharedPreferences sharedPrefs = getSharedPreferences();
    long downloadId = sharedPrefs.getLong(downloadGuid, INVALID_SYSTEM_DOWNLOAD_ID);
    if (downloadId != INVALID_SYSTEM_DOWNLOAD_ID) {
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.remove(downloadGuid);
        editor.apply();
    }
    return downloadId;
}

From source file:com.group13.androidsdk.mycards.NotificationService.java

private void rescheduleNotifications() {

    List<NotificationRule> rules = new ArrayList<>();
    Collections.addAll(rules, MyCardsDBManager.getInstance(this).getAllNotificationRules());
    rules.addAll(getCalendarAsNotificationRules());

    SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    long lastNotifMillis = prefs.getLong("lastNotifElapsedRealtime", -2 * FIFTEEN_MINUTES);
    SharedPreferences.Editor prefEditor = prefs.edit();

    MyCardsDBManager dbm = MyCardsDBManager.getInstance(this);
    if (!(Math.abs(lastNotifMillis - SystemClock.elapsedRealtime()) < FIFTEEN_MINUTES || dbm.getDoNotDisturb()
            || dateMatchesAnyRule(new Date(), rules)
            || dbm.getCardsForReviewBefore(new Date(), null).length == 0)) {
        lastNotifMillis = SystemClock.elapsedRealtime();
        prefEditor.putLong("lastNotifElapsedRealtime", lastNotifMillis);

        sendNotification();//from  w w w  .  j  a va  2  s .  com
    }

    Intent intent = new Intent(this, NotificationService.class);
    PendingIntent pi = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + FIFTEEN_MINUTES, pi);

    prefEditor.putLong("lastRunElapsedRealtime", SystemClock.elapsedRealtime());
    prefEditor.apply();
}

From source file:cn.newgxu.android.notty.ui.ServiceItems.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.service_items, container, false);
    SharedPreferences prefs = NottyApplication.getApp().getPrefs();
    Button login = (Button) v.findViewById(R.id.login);
    View gotoAuth = v.findViewById(R.id.go_to_auth);
    if (prefs.getLong(C._ID, 0) != 0) {
        login.setText(R.string.go_to_service);
        gotoAuth.setVisibility(View.GONE);
    } else {//from  w  w  w. j  a  v  a  2s .  co m
        gotoAuth.setOnClickListener(this);
    }
    login.setOnClickListener(this);
    return v;
}

From source file:com.playhaven.android.req.ContentRequest.java

@Override
protected UriComponentsBuilder createUrl(Context context) throws PlayHavenException {
    UriComponentsBuilder builder = super.createUrl(context);
    if (placementResId != -1)
        placementTag = context.getResources().getString(placementResId);

    if (placementTag != null)
        builder.queryParam("placement_id", placementTag);

    builder.queryParam("preload", isPreload() ? "1" : "0");

    SharedPreferences pref = PlayHaven.getPreferences(context);
    Calendar rightNow = Calendar.getInstance();
    Date date = rightNow.getTime();
    long end = date.getTime();
    long start = pref.getLong(SSTART, end);
    long stime = ((end - start) / 1000); // in seconds

    builder.queryParam(STIME, stime);/*from   w  ww.  j a v  a 2  s. c  o m*/

    // Store current stime for consumption by open requests
    SharedPreferences.Editor edit = pref.edit();
    edit.putLong(STIME, stime);
    edit.commit();

    return builder;
}

From source file:com.google.cloud.android.speech.AccessTokenLoader.java

@Override
public AccessToken loadInBackground() {
    final SharedPreferences prefs = getContext().getSharedPreferences(PREFS, Context.MODE_PRIVATE);
    String tokenValue = prefs.getString(PREF_ACCESS_TOKEN_VALUE, null);
    long expirationTimeMillis = prefs.getLong(PREF_ACCESS_TOKEN_EXPIRATION_TIME, -1);

    // Check if the current token is still valid for a while
    if (tokenValue != null && expirationTimeMillis > 0) {
        Date expirationTime = new Date(expirationTimeMillis);
        if (expirationTime.after(aWhileFromNow())) {
            return new AccessToken(tokenValue, expirationTime);
        }/* w  w w . ja v a2  s  .c  om*/
    }

    // ***** WARNING *****
    // In this sample, we load the credential from a JSON file stored in a raw resource folder
    // of this client app. You should never do this in your app. Instead, store the file in your
    // server and obtain an access token from there.
    // *******************
    final InputStream stream = getContext().getResources().openRawResource(R.raw.credential);
    try {
        final GoogleCredentials credentials = GoogleCredentials.fromStream(stream)
                .createScoped(ApiFragment.SCOPE);
        final AccessToken token = credentials.refreshAccessToken();
        prefs.edit().putString(PREF_ACCESS_TOKEN_VALUE, token.getTokenValue())
                .putLong(PREF_ACCESS_TOKEN_EXPIRATION_TIME, token.getExpirationTime().getTime()).apply();
        return token;
    } catch (IOException e) {
        Log.e(TAG, "Failed to obtain access token.", e);
    }
    return null;
}

From source file:cn.newgxu.android.notty.ui.UserServiceFragment.java

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    SharedPreferences prefs = NottyApplication.getApp().getPrefs();
    return new CursorLoader(getActivity(), Uri.parse(C.BASE_URI + C.NOTICES), null,
            C.notice.USER_ID + "=" + prefs.getLong(C._ID, 0), null, null);
}

From source file:com.example.groupproject.Model.LocationReceiver.java

private void addPoints(long pointValue) {
    //TODO:Make persistent
    android.content.SharedPreferences pm = _context.getSharedPreferences("GeoCat", 0);
    long points = pm.getLong("points", 100);
    points = points + pointValue;//from w  ww.  j ava2  s.  c  o  m
    android.content.SharedPreferences.Editor editor = pm.edit();
    editor.putLong("points", points);
    editor.commit();
    Log.d("addpoints", Long.toString(points));
}

From source file:com.twinflag.coofiletouch.AuthorityChecking.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ?/*  ww w  .j  a v  a 2  s . c o  m*/
    SharedPreferences mSharedPreferences = getSharedPreferences("checkTime", Context.MODE_PRIVATE);
    long lastCheckTimeMillis = mSharedPreferences.getLong("lastCheckTime", 0);

    long currentMillis = System.currentTimeMillis();

    if (needFetchLincenseFromNet(currentMillis, lastCheckTimeMillis)
            || AuthorityUtil.getInstance().isAuthorityExpired()) {
        fetchLicenseFromInternet();
    } else {
        Log.i(TAG, "license is valid !");
        finish();
        Intent intent = new Intent(AuthorityChecking.this, MainActivity.class);
        startActivity(intent);
    }
}

From source file:com.Candy.ota.CandyOTA.java

private void setInitialUpdateInterval() {
    SharedPreferences prefs = this.getActivity().getSharedPreferences(LAST_INTERVAL, 0);
    long value = prefs.getLong(LAST_INTERVAL, 0);
    //set interval to 12h if user starts first time SlimOTA and it was not installed by system before
    //yes ask lazy tarak....he has this case ;)
    if (value == 0) {
        UpdateListener.interval = AlarmManager.INTERVAL_HALF_DAY;
        prefs.edit().putLong(LAST_INTERVAL, UpdateListener.interval).apply();
        WakefulIntentService.scheduleAlarms(new UpdateListener(), getActivity(), false);
    }// w  w  w .  j  av a2s  .  co m
}

From source file:com.ultramegatech.ey.UpdateService.java

@Override
protected void onHandleIntent(Intent intent) {
    if (!HttpHelper.isConnected(this)) {
        return;// w ww. j  ava  2  s.c o m
    }

    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

    final long lastCheck = preferences.getLong(KEY_LAST_CHECK, 0);
    final long now = System.currentTimeMillis();
    if (now - lastCheck < CHECK_INTERVAL) {
        return;
    }

    Log.v(TAG, "Checking for updates");

    final int version = preferences.getInt(KEY_VERSION, 0);
    final int newVersion = HttpHelper.getVersion();
    if (newVersion > version) {
        Log.v(TAG, "Downloading updates...");

        final ContentValues[] valuesArray = fetchElementData(newVersion);
        if (valuesArray == null) {
            return;
        }

        final ContentResolver cr = getContentResolver();
        Uri uri;
        int changed;
        for (int i = 0; i < valuesArray.length; i++) {
            uri = ContentUris.withAppendedId(Elements.CONTENT_URI_NUMBER,
                    valuesArray[i].getAsLong(Elements.NUMBER));
            changed = cr.update(uri, valuesArray[i], null, null);

            if (changed == 0) {
                cr.insert(Elements.CONTENT_URI, valuesArray[i]);
            }
        }

        preferences.edit().putInt(KEY_VERSION, newVersion).commit();

        Log.v(TAG, "Update completed successfully");
    }

    preferences.edit().putLong(KEY_LAST_CHECK, System.currentTimeMillis()).commit();
}