List of usage examples for android.content SharedPreferences getLong
long getLong(String key, long defValue);
From source file:com.upenn.chriswang1990.sunshine.sync.SunshineSyncAdapter.java
private void notifyWeather() { Context context = getContext(); //checking the last update and notify if it' the first of the day SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String displayNotificationsKey = context.getString(R.string.pref_enable_notifications_key); boolean displayNotifications = prefs.getBoolean(displayNotificationsKey, Boolean.parseBoolean(context.getString(R.string.pref_enable_notifications_default))); if (displayNotifications) { String lastNotificationKey = context.getString(R.string.pref_last_notification); long lastSync = prefs.getLong(lastNotificationKey, 0); if (System.currentTimeMillis() - lastSync >= DAY_IN_MILLIS) { // Last sync was more than 1 day ago, let's send a notification with the weather. String locationQuery = Utility.getPreferredLocation(context); String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC"; Uri weatherUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(locationQuery, Utility.normalizeDate(System.currentTimeMillis() / 1000, Utility.getTimezoneID(context))); // we'll query our contentProvider, as always Cursor cursor = context.getContentResolver().query(weatherUri, NOTIFY_WEATHER_PROJECTION, null, null, null);/* w w w . j a v a 2 s . co m*/ if (cursor.moveToFirst()) { String cityName = cursor.getString(INDEX_CITY_NAME); int weatherId = cursor.getInt(INDEX_WEATHER_ID); double high = cursor.getDouble(INDEX_MAX_TEMP); double low = cursor.getDouble(INDEX_MIN_TEMP); String desc = cursor.getString(INDEX_SHORT_DESC); int iconId = Utility.getIconResourceForWeatherCondition(weatherId); Resources resources = context.getResources(); Bitmap largeIcon = BitmapFactory.decodeResource(resources, Utility.getArtResourceForWeatherCondition(weatherId)); String title = context.getString(R.string.app_name); // Define the text of the forecast. String contentText = String.format(context.getString(R.string.format_notification), cityName, desc, Utility.formatTemperature(context, high), Utility.formatTemperature(context, low)); // NotificationCompatBuilder is a very convenient way to build backward-compatible // notifications. Just throw in some data. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setColor(context.getResources().getColor(R.color.primary_light)).setSmallIcon(iconId) .setLargeIcon(largeIcon).setContentTitle(title).setContentText(contentText); // Make something interesting happen when the user clicks on the notification. // In this case, opening the app is sufficient. Intent resultIntent = new Intent(context, MainActivity.class); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getContext() .getSystemService(Context.NOTIFICATION_SERVICE); // WEATHER_NOTIFICATION_ID allows you to update the notification later on. mNotificationManager.notify(WEATHER_NOTIFICATION_ID, mBuilder.build()); //refreshing last sync SharedPreferences.Editor editor = prefs.edit(); editor.putLong(lastNotificationKey, System.currentTimeMillis()); editor.apply(); } cursor.close(); } } }
From source file:com.jackie.sunshine.app.sync.SunshineSyncAdapter.java
private void notifyWeather() { Context context = getContext(); //checking the last update and notify if it' the first of the day SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String displayNotificationsKey = context.getString(R.string.pref_enable_notifications_key); boolean needNotification = prefs.getBoolean(displayNotificationsKey, Boolean.valueOf(context.getString(R.string.pref_enable_notifications_default))); if (needNotification) { String lastNotificationKey = context.getString(R.string.pref_last_notification); long lastSync = prefs.getLong(lastNotificationKey, 0); if (System.currentTimeMillis() - lastSync >= DAY_IN_MILLIS) { // Last sync was more than 1 day ago, let's send a notification with the weather. String locationQuery = Utility.getPreferredLocation(context); Uri weatherUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(locationQuery, System.currentTimeMillis()); // we'll query our contentProvider, as always Cursor cursor = context.getContentResolver().query(weatherUri, NOTIFY_WEATHER_PROJECTION, null, null, null);/* ww w . jav a 2 s. c om*/ if (cursor.moveToFirst()) { int weatherId = cursor.getInt(INDEX_WEATHER_ID); double high = cursor.getDouble(INDEX_MAX_TEMP); double low = cursor.getDouble(INDEX_MIN_TEMP); String desc = cursor.getString(INDEX_SHORT_DESC); int iconId = Utility.getIconResourceForWeatherCondition(weatherId); String title = context.getString(R.string.app_name); // Define the text of the forecast. String contentText = String.format(context.getString(R.string.format_notification), desc, Utility.formatTemperature(context, high), Utility.formatTemperature(context, low)); //build your notification here. Intent intent = new Intent(context, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(intent); PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new Notification.Builder(context).setSmallIcon(iconId) .setContentTitle(title).setContentText(contentText).setContentIntent(pendingIntent) .build(); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(WEATHER_NOTIFICATION_ID, notification); //refreshing last sync SharedPreferences.Editor editor = prefs.edit(); editor.putLong(lastNotificationKey, System.currentTimeMillis()); editor.apply(); } cursor.close(); } } }
From source file:org.alfresco.mobile.android.application.operations.sync.SynchroManager.java
/** * Start a sync if the last activity time is greater than 1 hour. */// w ww .ja v a 2 s . co m public void cronSync(Account account) { if (account == null) { return; } long now = new Date().getTime(); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(mAppContext); long lastTime = sharedPref.getLong(LAST_SYNC_ACTIVATED_AT + account.getId(), now); if ((lastTime + 3600000) < now && canSync(account)) { sync(account); } }
From source file:in.shick.diode.settings.RedditSettings.java
public void loadRedditPreferences(Context context, HttpClient client) { // Session/*from w ww .ja va 2s. co m*/ SharedPreferences sessionPrefs = PreferenceManager.getDefaultSharedPreferences(context); this.setUsername(sessionPrefs.getString("username", null)); this.setModhash(sessionPrefs.getString("modhash", null)); String cookieValue = sessionPrefs.getString("reddit_sessionValue", null); String cookieDomain = sessionPrefs.getString("reddit_sessionDomain", null); String cookiePath = sessionPrefs.getString("reddit_sessionPath", null); long cookieExpiryDate = sessionPrefs.getLong("reddit_sessionExpiryDate", -1); if (cookieValue != null) { BasicClientCookie redditSessionCookie = new BasicClientCookie("reddit_session", cookieValue); redditSessionCookie.setDomain(cookieDomain); redditSessionCookie.setPath(cookiePath); if (cookieExpiryDate != -1) redditSessionCookie.setExpiryDate(new Date(cookieExpiryDate)); else redditSessionCookie.setExpiryDate(null); this.setRedditSessionCookie(redditSessionCookie); RedditIsFunHttpClientFactory.getCookieStore().addCookie(redditSessionCookie); try { CookieSyncManager.getInstance().sync(); } catch (IllegalStateException ex) { if (Constants.LOGGING) Log.e(TAG, "CookieSyncManager.getInstance().sync()", ex); } } // Default subreddit String homepage = sessionPrefs.getString(Constants.PREF_HOMEPAGE, Constants.FRONTPAGE_STRING).trim(); if (StringUtils.isEmpty(homepage)) this.setHomepage(Constants.FRONTPAGE_STRING); else this.setHomepage(homepage); //Browser Settings this.setOverwriteUA(sessionPrefs.getBoolean(Constants.PREF_OVERWRITE_UA, false)); this.setUseragent(sessionPrefs.getString(Constants.BROWSER_UA, Constants.BROWSER_UA_STRING)); this.setLoadJS(sessionPrefs.getBoolean(Constants.PREF_LOAD_JS, true)); this.setLoadPlugins(sessionPrefs.getBoolean(Constants.PREF_LOAD_PLUGINS, true)); this.setLoadImgurImagesDirectly(sessionPrefs.getBoolean(Constants.PREF_IMGUR_DIRECT, true)); this.setLoadVredditLinksDirectly(sessionPrefs.getBoolean(Constants.PREF_VREDDIT_DIRECT, false)); // Use external browser instead of BrowserActivity this.setUseExternalBrowser(sessionPrefs.getBoolean(Constants.PREF_USE_EXTERNAL_BROWSER, false)); // Show confirmation dialog when backing out of root Activity this.setConfirmQuitOrLogout(sessionPrefs.getBoolean(Constants.PREF_CONFIRM_QUIT, true)); // Save reddit history to Browser history this.setSaveHistory(sessionPrefs.getBoolean(Constants.PREF_SAVE_HISTORY, true)); // Whether to always show the next/previous buttons, or only at bottom of list this.setAlwaysShowNextPrevious(sessionPrefs.getBoolean(Constants.PREF_ALWAYS_SHOW_NEXT_PREVIOUS, true)); // Comments sort order this.setCommentsSortByUrl(sessionPrefs.getString(Constants.PREF_COMMENTS_SORT_BY_URL, Constants.CommentsSort.SORT_BY_BEST_URL)); // Theme and text size this.setTheme(Util.getThemeResourceFromPrefs( sessionPrefs.getString(Constants.PREF_THEME, Constants.PREF_THEME_LIGHT), sessionPrefs.getString(Constants.PREF_TEXT_SIZE, Constants.PREF_TEXT_SIZE_MEDIUM))); // Comment guide lines this.setShowCommentGuideLines(sessionPrefs.getBoolean(Constants.PREF_SHOW_COMMENT_GUIDE_LINES, true)); // Rotation this.setRotation(RedditSettings.Rotation .valueOf(sessionPrefs.getString(Constants.PREF_ROTATION, Constants.PREF_ROTATION_UNSPECIFIED))); // Thumbnails this.setLoadThumbnails(sessionPrefs.getBoolean(Constants.PREF_LOAD_THUMBNAILS, true)); // Thumbnails on Wifi this.setLoadThumbnailsOnlyWifi(sessionPrefs.getBoolean(Constants.PREF_LOAD_THUMBNAILS_ONLY_WIFI, false)); // NSFW this.setShowNSFW(sessionPrefs.getBoolean(Constants.PREF_SHOW_NSFW, Constants.PREF_SHOW_NSFW_DEFAULT)); // Notifications this.setMailNotificationStyle(sessionPrefs.getString(Constants.PREF_MAIL_NOTIFICATION_STYLE, Constants.PREF_MAIL_NOTIFICATION_STYLE_DEFAULT)); this.setMailNotificationService(sessionPrefs.getString(Constants.PREF_MAIL_NOTIFICATION_SERVICE, Constants.PREF_MAIL_NOTIFICATION_SERVICE_OFF)); this.setFilters(sessionPrefs.getString(Constants.PREF_REDDIT_FILTERS, null)); }
From source file:com.anjalimacwan.fragment.NoteEditFragment.java
@Override public void onResume() { super.onResume(); SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); // Disable restoring drafts if user launched Notepad through a share intent if (!listener.isShareIntent()) { if (filename.equals("draft")) { // Restore draft preferences draftName = sharedPref.getLong("draft-name", 0); isSavedNote = sharedPref.getBoolean("is-saved-note", false); // Restore filename of draft filename = Long.toString(draftName); // Reload old file into memory, so that correct contentsOnLoad is set if (isSavedNote) { try { contentsOnLoad = listener.loadNote(filename); } catch (IOException e) { showToast(R.string.error_loading_note); finish(null);/*from w ww . j av a 2 s.c o m*/ } } else contentsOnLoad = ""; // Notify the user that a draft has been restored showToast(R.string.draft_restored); } // Clear draft preferences SharedPreferences.Editor editor = sharedPref.edit(); editor.remove("draft-name"); editor.remove("is-saved-note"); editor.remove("draft-contents"); editor.apply(); } // Change window title String title; if (isSavedNote) try { title = listener.loadNoteTitle(filename); } catch (IOException e) { title = getResources().getString(R.string.edit_note); } else title = getResources().getString(R.string.action_new); getActivity().setTitle(title); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(title, null, ContextCompat.getColor(getActivity(), R.color.primary)); getActivity().setTaskDescription(taskDescription); } SharedPreferences pref = getActivity().getSharedPreferences(getActivity().getPackageName() + "_preferences", Context.MODE_PRIVATE); directEdit = pref.getBoolean("direct_edit", false); }
From source file:com.notepadlite.NoteEditFragment.java
@Override public void onResume() { super.onResume(); SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); // Disable restoring drafts if user launched Notepad through a share intent if (!listener.isShareIntent()) { if (filename.equals("draft")) { // Restore draft preferences draftName = sharedPref.getLong("draft-name", 0); isSavedNote = sharedPref.getBoolean("is-saved-note", false); // Restore filename of draft filename = Long.toString(draftName); // Reload old file into memory, so that correct contentsOnLoad is set if (isSavedNote) { try { contentsOnLoad = listener.loadNote(filename); } catch (IOException e) { showToast(R.string.error_loading_note); finish(null);/*from ww w . j av a2 s. co m*/ } } else contentsOnLoad = ""; // Notify the user that a draft has been restored showToast(R.string.draft_restored); } // Clear draft preferences SharedPreferences.Editor editor = sharedPref.edit(); editor.remove("draft-name"); editor.remove("is-saved-note"); editor.remove("draft-contents"); editor.apply(); } // Change window title String title; if (isSavedNote) try { title = listener.loadNoteTitle(filename); } catch (IOException e) { title = getResources().getString(R.string.edit_note); } else title = getResources().getString(R.string.action_new); getActivity().setTitle(title); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(title, null, getResources().getColor(R.color.primary)); getActivity().setTaskDescription(taskDescription); } SharedPreferences pref = getActivity().getSharedPreferences(getActivity().getPackageName() + "_preferences", Context.MODE_PRIVATE); directEdit = pref.getBoolean("direct_edit", false); }
From source file:com.hhunj.hhudata.ForegroundService.java
private void resetForNewQuery() { networkThread = null;//from ww w . ja va2s .co m SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); DBUtils dbUtils = new DBUtils(this); long hostid = pref.getLong("hostid", -1); Host h = dbUtils.hostDelegate.getHost(hostid); if (h == null) { h = Host.getdefaultHost(); } m_address = h.getHost(); //m_rss = Constants.getRss(pref); m_port = h.getPort(); m_aquirespan = h.getAquirespan(); m_alamspan = h.getAlamspan(); m_workStartTime = h.getWorkStartTime(); m_workEndTime = h.getWorkEndTime(); m_alarmInWorktime = h.getAlarmInWorktime(); mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); }
From source file:com.example.app_2.activities.ImageGridActivity.java
@Override public boolean onOptionsItemSelected(MenuItem item) { // Pass the event to ActionBarDrawerToggle, if it returns // true, then it has handled the app icon touch event if (!mDualPane) if (mDrawerToggle.onOptionsItemSelected(item)) { return true; }//from ww w. j av a2 s .c o m // Handle presses on the action bar items switch (item.getItemId()) { case R.id.action_add_image:// TODO dodawanie nowych obrazkw Intent bind_intent = new Intent(this, BindImagesToCategoryActivity.class); if (actual_category_fk.getCategoryId() == Database.getMainRootFk()) { Toast.makeText(getApplicationContext(), "Tutaj nie mona dodawa obrazkw, wybierz najpierw uytkownika", Toast.LENGTH_LONG).show(); return true; } bind_intent.putExtra("executing_category_id", actual_category_fk.getCategoryId()); startActivity(bind_intent); return true; case R.id.action_add_new_image: Intent new_imageIntent = new Intent(this, AddImageActivity.class); Bundle bundle = new Bundle(); bundle.putLong("cat_fk", actual_category_fk.getCategoryId()); new_imageIntent.putExtras(bundle); startActivity(new_imageIntent); return true; case R.id.action_settings: Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out); finish(); return true; case R.id.action_logout: igf.mEditMode = true; SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()); SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("USER", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); //zapisanie usawie uytkownika do bazy Uri uri = Uri.parse(UserContract.CONTENT_URI + "/" + sharedPref.getLong("logged_user_id", 0)); ContentValues cv = new ContentValues(); cv.put(UserContract.Columns.FONT_SIZE, sp.getInt("pref_img_desc_font_size", 15)); cv.put(UserContract.Columns.IMG_SIZE, sp.getInt("pref_img_size", 100)); cv.put(UserContract.Columns.CAT_BACKGROUND, String.valueOf(sp.getInt("category_view_background", 0xff33b5e5))); cv.put(UserContract.Columns.CONTEXT_CAT_BACKGROUND, String.valueOf(sp.getInt("context_category_view_background", 0xffe446ff))); getContentResolver().update(uri, cv, null, null); editor.putLong("logged_user_root", Database.getMainRootFk()); editor.putLong("logged_user_id", 0); editor.commit(); fragmentsHistory.clear(); Intent i = getIntent(); finish(); startActivity(i); replaceCategory(Database.getMainRootFk(), 0, false); return true; case R.id.action_login_user: Intent login_intent = new Intent(this, UserLoginActivity.class); startActivity(login_intent); finish(); fragmentsHistory.clear(); return true; default: return super.onOptionsItemSelected(item); } }
From source file:com.katamaditya.apps.weather4u.weathersync.Weather4USyncAdapter.java
public void notifyWeather() { Context context = getContext(); //checking the last update and notify if it' the first of the day SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String displayNotificationsKey = context.getString(R.string.pref_enable_notifications_key); boolean displayNotifications = prefs.getBoolean(displayNotificationsKey, Boolean.parseBoolean(context.getString(R.string.pref_enable_notifications_default))); if (displayNotifications) { String lastNotificationKey = context.getString(R.string.pref_last_notification); long lastSync = prefs.getLong(lastNotificationKey, 0); if (System.currentTimeMillis() - lastSync >= getNotificationTimeGap()) { // Last sync was more than 1 day ago, let's send a notification with the weather. String locationQuery = WeatherUtil.getPreferredLocation(context); Uri weatherUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(locationQuery, System.currentTimeMillis()); // we'll query our contentProvider, as always Cursor cursor = context.getContentResolver().query(weatherUri, NOTIFY_WEATHER_PROJECTION, null, null, null);//from ww w.j a v a 2 s . c o m if (cursor.moveToFirst()) { int weatherId = cursor.getInt(INDEX_WEATHER_ID); double high = cursor.getDouble(INDEX_MAX_TEMP); double low = cursor.getDouble(INDEX_MIN_TEMP); String desc = cursor.getString(INDEX_SHORT_DESC); int iconId = WeatherUtil.getIconResourceForWeatherCondition(weatherId); Resources resources = context.getResources(); Bitmap largeIcon = BitmapFactory.decodeResource(resources, WeatherUtil.getArtResourceForWeatherCondition(weatherId)); String title = getTitle(); // Define the text of the forecast. String contentText = String.format(context.getString(R.string.format_notification), desc, WeatherUtil.formatTemperature(context, high), WeatherUtil.formatTemperature(context, low)); // NotificationCompatBuilder is a very convenient way to build backward-compatible // notifications. Just throw in some data. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext()) .setColor(resources.getColor(R.color.weather4u_light_blue)).setSmallIcon(iconId) .setLargeIcon(largeIcon).setContentTitle(title).setContentText(contentText); mBuilder.setAutoCancel(true); // Make something interesting happen when the user clicks on the notification. // In this case, opening the app is sufficient. Intent resultIntent = new Intent(context, MainActivity.class); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getContext() .getSystemService(Context.NOTIFICATION_SERVICE); // WEATHER_NOTIFICATION_ID allows you to update the notification later on. mNotificationManager.notify(WEATHER_NOTIFICATION_ID, mBuilder.build()); //refreshing last sync SharedPreferences.Editor editor = prefs.edit(); editor.putLong(lastNotificationKey, System.currentTimeMillis()); editor.commit(); } cursor.close(); } } }
From source file:org.spontaneous.trackservice.RemoteService.java
private Long getUserId() { Long userId = null;// www.j av a 2s .co m SharedPreferences sharedPrefs = getSharedPreferences(Constants.PREFERENCES, MODE_PRIVATE); if (sharedPrefs != null) { userId = sharedPrefs.getLong(Constants.PREF_USERID, -1L); } return userId; }