下面列出了android.app.Application#getSharedPreferences ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public BackupViewModel(@NonNull Application application) {
super(application);
mFilterPrefs = application.getSharedPreferences("backup_filter", Context.MODE_PRIVATE);
BackupPackagesFilterConfig filterConfig = new BackupPackagesFilterConfig(mFilterPrefs);
mBackupFilterConfig.setValue(filterConfig);
mComplexFilterConfig = filterConfig.toComplexFilterConfig(application);
mPackagesLiveData.setValue(new ArrayList<>());
mBackupManager = DefaultBackupManager.getInstance(getApplication());
mBackupRepoPackagesObserver = (packages) -> search(mCurrentSearchQuery);
mBackupManager.getApps().observeForever(mBackupRepoPackagesObserver);
mLiveFilterApplier.asLiveData().observeForever(mLiveFilterObserver);
mBackupFilterConfig.setValue(new BackupPackagesFilterConfig(mComplexFilterConfig));
}
public void init(Application context, TrackerConfiguration config) {
if (config == null) {
throw new IllegalArgumentException("config can't be null");
}
isInit = true;
this.context = context;
setTrackerConfig(config);
preferences = context.getSharedPreferences(context.getPackageName(), MODE_PRIVATE);
if (preferences.getBoolean(KEY_IS_NEW_DEVICE, true) && !TextUtils.isEmpty(config.getNewDeviceUrl())) {
submitDeviceInfo();
}
context.registerActivityLifecycleCallbacks(new ActivityLifecycleListener());
if (config.getUploadCategory() == UPLOAD_CATEGORY.REAL_TIME) {
UploadEventService.enter(context, config.getHostName(), config.getHostPort(), null);
} else {
if (!requestConfig) {
startRequestConfig();
}
}
}
@Before
public void setUp() throws Exception {
final Application application = RuntimeEnvironment.application;
sharedPreferences = application.getSharedPreferences("test", Context.MODE_PRIVATE);
sharedPreferences.edit().clear().apply();
//dataSaver = new SharedPreferencesDataSaver(sharedPreferences);
dataSaver = new PaperDataSaver(application);
dataSaver = spy(dataSaver);
AndroidNoSql.initWith(
dataSaver
);
noSql = NoSql.getInstance();
noSql.reset();
}
@Test
public void testModule_shouldReturnNamedSharedPreferences() throws Exception {
// GIVEN
Application application = ApplicationProvider.getApplicationContext();
String sharedPreferencesName = "test";
String itemKey = "isValid";
SharedPreferences sharedPreferences =
application.getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE);
sharedPreferences.edit().putBoolean(itemKey, true).commit();
Scope appScope = Toothpick.openScope(application);
appScope.installModules(new SmoothieApplicationModule(application, sharedPreferencesName));
// WHEN
SharedPreferences sharedPreferencesFromScope = appScope.getInstance(SharedPreferences.class);
// THEN
assertThat(sharedPreferencesFromScope.getBoolean(itemKey, false), is(true));
}
/**
* Helper method for getting a boolean value from the apps stored preferences.
*
* @param preference the preference key.
* @param defaultValue the default value to return if the key is not found.
* @return the value stored or the default if the stored value is not found.
*/
public boolean getAndSetBooleanPreference(String preference, boolean defaultValue) {
Application application = (Application) firebaseApp.getApplicationContext();
SharedPreferences preferences =
application.getSharedPreferences(PREFERENCES_PACKAGE_NAME, Context.MODE_PRIVATE);
// Value set at runtime overrides anything else, but default to defaultValue.
if (preferences.contains(preference)) {
boolean result = preferences.getBoolean(preference, defaultValue);
return result;
}
// No preferences set yet - use and set defaultValue.
setBooleanPreference(preference, defaultValue);
return defaultValue;
}
/**
* Helper method for getting a boolean value from the apps stored preferences.
*
* @param preference the preference key.
* @param defaultValue the default value to return if the key is not found.
* @return the value stored or the default if the stored value is not found.
*/
public boolean getBooleanPreference(String preference, boolean defaultValue) {
Application application = (Application) firebaseApp.getApplicationContext();
SharedPreferences preferences =
application.getSharedPreferences(PREFERENCES_PACKAGE_NAME, Context.MODE_PRIVATE);
// Value set at runtime overrides anything else, but default to defaultValue.
if (preferences.contains(preference)) {
boolean result = preferences.getBoolean(preference, defaultValue);
return result;
}
// No preferences set yet - use defaultValue.
return defaultValue;
}
/**
* Helper method for getting a boolean value from the apps stored preferences.
*
* @param preference the preference key.
* @return whether the preference has been set or not
*/
public boolean isPreferenceSet(String preference) {
Application application = (Application) firebaseApp.getApplicationContext();
SharedPreferences preferences =
application.getSharedPreferences(PREFERENCES_PACKAGE_NAME, Context.MODE_PRIVATE);
return preferences.contains(preference);
}
/**
* Saves the provided identity id as the currently active id in the app preferences.
*
* @param application The caller's application object.
*/
public static void saveCurrentId(Application application, long newIdentityId) {
SharedPreferences sharedPref = application.getSharedPreferences(APPS_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putLong(CURRENT_ID, newIdentityId);
editor.apply();
}
/**
* save OpenAtlas runtime info to sharedPreference
**/
public static void saveAtlasInfoBySharedPreferences(Application application) {
Map<String, String> concurrentHashMap = new ConcurrentHashMap<String, String>();
concurrentHashMap.put(getPackageInfo(application).versionName, "dexopt");
SharedPreferences sharedPreferences = application.getSharedPreferences(OpenAtlasInternalConstant.OPENATLAS_CONFIGURE, Context.MODE_PRIVATE);
if (sharedPreferences == null) {
sharedPreferences = application.getSharedPreferences(OpenAtlasInternalConstant.OPENATLAS_CONFIGURE, Context.MODE_PRIVATE);
}
Editor edit = sharedPreferences.edit();
for (String key : concurrentHashMap.keySet()) {
edit.putString(key, concurrentHashMap.get(key));
}
edit.commit();
}
public RNPushNotificationHelper(Application context) {
this.context = context;
this.scheduledNotificationsPersistence = context.getSharedPreferences(RNPushNotificationHelper.PREFERENCES_KEY, Context.MODE_PRIVATE);
}
ListRemoteViewsFactory(RemoteViewsService remoteViewsService, Application app, Intent intent) {
this.app = (PalmApp) app;
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
sharedPreferences = app.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_MULTI_PROCESS);
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@Provides
@Singleton
public SharedPreferences providesSharedPreferences(Application application){
return application.getSharedPreferences("user", Context.MODE_PRIVATE);
}
@Inject
MessageStateStorage(Application application) {
this.preferences = application.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
}
public ForceUpdate(Application gApplication,
boolean gDebug,
VersionProvider gForcedVersionProvider, int gForcedVersionInterval,
VersionProvider gRecommendedVersionProvider, int gRecommendedVersionInterval,
VersionProvider gExcludedVersionListProvider, int gExcludedVersionListInterval,
VersionProvider gCurrentVersionProvider,
UpdateView gForcedVersionView,
UpdateView gRecommendedVersionView,
List<Class<?>> gForceUpdateActivities) {
if(alreadyInstantiated) {
throw new RuntimeException("ForceUpdate library is already initialized.");
}
String permission = "android.permission.INTERNET";
int res = gApplication.checkCallingOrSelfPermission(permission);
if(res != PackageManager.PERMISSION_GRANTED) {
throw new RuntimeException("Internet permission is necessary for version checks.");
}
if(!gDebug && (gForcedVersionInterval < 60 || gRecommendedVersionInterval < 60 || gExcludedVersionListInterval < 60)) {
throw new RuntimeException("Minimal fetch interval is 60s");
}
application = gApplication;
minAllowedVersionProvider = gForcedVersionProvider;
minAllowedVersionInterval = gForcedVersionInterval;
recommendedVersionProvider = gRecommendedVersionProvider;
recommendedVersionInterval = gRecommendedVersionInterval;
excludedVersionProvider = gExcludedVersionListProvider;
excludedVersionInterval = gExcludedVersionListInterval;
currentVersionProvider = gCurrentVersionProvider;
forcedUpdateView = gForcedVersionView;
recommendedUpdateView = gRecommendedVersionView;
forceUpdateActivities = gForceUpdateActivities;
sharedPreferences = gApplication.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
alreadyInstantiated = true;
}
public PreferencesManager(Application application){
sharedPreferences = application.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
}
public RNPushNotificationHelper(Application context) {
this.context = context;
this.config = new RNPushNotificationConfig(context);
this.scheduledNotificationsPersistence = context.getSharedPreferences(RNPushNotificationHelper.PREFERENCES_KEY, Context.MODE_PRIVATE);
}
@Provides @Singleton SharedPreferences provideSharedPreferences(Application app) {
return app.getSharedPreferences("view-inspector", MODE_PRIVATE);
}
@Provides @Singleton SharedPreferences provideSharedPreferences(Application app) {
return app.getSharedPreferences("data-inspector", MODE_PRIVATE);
}
@Provides
@Singleton
SharedPreferences provideSharedPreferences(Application app) {
return app.getSharedPreferences("daggerdemo", Context.MODE_PRIVATE);
}
@Provides
@ApplicationScope
SharedPreferences provideSharedPreferences(Application app) {
return app.getSharedPreferences("u2020", MODE_PRIVATE);
}