android.content.SharedPreferences.getBoolean()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(11.5k)|赞(0)|评价(0)|浏览(140)

本文整理了Java中android.content.SharedPreferences.getBoolean()方法的一些代码示例,展示了SharedPreferences.getBoolean()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SharedPreferences.getBoolean()方法的具体详情如下:
包路径:android.content.SharedPreferences
类名称:SharedPreferences
方法名:getBoolean

SharedPreferences.getBoolean介绍

[英]Retrieve a boolean value from the preferences.
[中]从首选项中检索布尔值。

代码示例

代码示例来源:origin: stackoverflow.com

final String PREFS_NAME = "MyPrefsFile";

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

if (settings.getBoolean("my_first_time", true)) {
  //the app is being launched for first time, do something        
  Log.d("Comments", "First time");

       // first time task

  // record the fact that the app has been started at least once
  settings.edit().putBoolean("my_first_time", false).commit(); 
}

代码示例来源:origin: robolectric/robolectric

@Test
public void commit_shouldStoreValues() throws Exception {
 editor.commit();
 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
 assertTrue(anotherSharedPreferences.getBoolean("boolean", false));
 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f);
 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2);
 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L);
 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar");
 assertThat(anotherSharedPreferences.getStringSet("stringSet", null)).isEqualTo(stringSet);
}

代码示例来源:origin: stackoverflow.com

public class MyActivity extends Activity {

  SharedPreferences prefs = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Perhaps set content view here

    prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
  }

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

    if (prefs.getBoolean("firstrun", true)) {
      // Do first run stuff here then set 'firstrun' as false
      // using the following line to edit/commit prefs
      prefs.edit().putBoolean("firstrun", false).commit();
    }
  }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void commit_shouldRemoveValuesThenSetValues() throws Exception {
 editor.putString("deleteMe", "foo").commit();
 editor.remove("deleteMe");
 editor.putString("dontDeleteMe", "baz");
 editor.remove("dontDeleteMe");
 editor.commit();
 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
 assertThat(anotherSharedPreferences.getBoolean("boolean", false)).isTrue();
 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f);
 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2);
 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L);
 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar");
 assertThat(anotherSharedPreferences.getString("deleteMe", "awol")).isEqualTo("awol");
 assertThat(anotherSharedPreferences.getString("dontDeleteMe", "oops")).isEqualTo("oops");
}

代码示例来源:origin: stackoverflow.com

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
boolean firstRun = p.getBoolean(PREFERENCE_FIRST_RUN, true);
p.edit().putBoolean(PREFERENCE_FIRST_RUN, false).commit();

代码示例来源:origin: robolectric/robolectric

@Test
public void commit_shouldClearThenSetValues() throws Exception {
 editor.putString("deleteMe", "foo");
 editor.clear();
 editor.putString("dontDeleteMe", "baz");
 editor.commit();
 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
 assertTrue(anotherSharedPreferences.getBoolean("boolean", false));
 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f);
 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2);
 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L);
 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar");
 // Android always calls clear before put on any open editor, so here "foo" is preserved rather than cleared.
 assertThat(anotherSharedPreferences.getString("deleteMe", "awol")).isEqualTo("foo");
 assertThat(anotherSharedPreferences.getString("dontDeleteMe", "oops")).isEqualTo("baz");
}

代码示例来源:origin: stackoverflow.com

private SharedPreferences mPreferences;
....
boolean firstTime = mPreferences.getBoolean("firstTime", true);
if (firstTime) { 
  SharedPreferences.Editor editor = mPreferences.edit();
  editor.putBoolean("firstTime", false);
  editor.commit();
  showMiddleActivity();
}

代码示例来源:origin: chrisk44/Hijacker

monstart = pref.getBoolean("monstart", monstart);
enable_monMode = pref.getString("enable_monMode", enable_monMode);
disable_monMode = pref.getString("disable_monMode", disable_monMode);
enable_on_airodump = pref.getBoolean("enable_on_airodump", enable_on_airodump);
show_notif = pref.getBoolean("show_notif", show_notif);
show_details = pref.getBoolean("show_details", show_details);
airOnStartup = pref.getBoolean("airOnStartup", airOnStartup);
debug = pref.getBoolean("debug", debug);
watchdog = pref.getBoolean("watchdog", watchdog);
target_deauth = pref.getBoolean("target_deauth", target_deauth);
delete_extra = pref.getBoolean("delete_extra", delete_extra);
try{
  always_cap = pref.getBoolean("always_cap", always_cap);
}catch(ClassCastException e){
  pref_edit.putBoolean("always_cap", false);
  pref_edit.commit();
cont_on_fail = pref.getBoolean("cont_on_fail", cont_on_fail);
update_on_startup = pref.getBoolean("update_on_startup", update_on_startup);
band = Integer.parseInt(pref.getString("band", Integer.toString(band)));
show_client_count = pref.getBoolean("show_client_count", show_client_count);

代码示例来源:origin: stackoverflow.com

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
 boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false);
 if(!previouslyStarted) {
   SharedPreferences.Editor edit = prefs.edit();
   edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);
   edit.commit();
   showHelp();
 }

代码示例来源:origin: stackoverflow.com

SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
if (isFirstRun)
{
  // Code to run once
  SharedPreferences.Editor editor = wmbPreference.edit();
  editor.putBoolean("FIRSTRUN", false);
  editor.commit();
}

代码示例来源:origin: stackoverflow.com

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) {
  // run your one time code
  SharedPreferences.Editor editor = prefs.edit();
  editor.putBoolean("firstTime", true);
  editor.commit();
}

代码示例来源:origin: stackoverflow.com

SharedPreferences runCheck = PreferenceManager.getSharedPreferences("hasRunBefore", 0); //load the preferences
Boolean hasRun = runCheck.getBoolean("hasRun", false); //see if it's run before, default no
if (!hasRun) {
  SharedPreferences settings = getSharedPreferences("hasRunBefore", 0);
  SharedPreferences.Editor edit = settings.edit();
  edit.putBoolean("hasRun", true); //set to has run
  edit.commit(); //apply
  //code for if this is the first time the app has run
}
else {
  //code if the app HAS run before
}

代码示例来源:origin: stackoverflow.com

public void onCreate(Bundle savedInstanceState) {
 // don't forget to call super method.
 super.onCreate(savedInstanceState);

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
 if (!prefs.getBoolean("firstTime", false)) {
  // <---- run your one time code here
  databaseSetup();

  // mark first time has runned.
  SharedPreferences.Editor editor = prefs.edit();
  editor.putBoolean("firstTime", true);
  editor.commit();
 }
}

代码示例来源:origin: stackoverflow.com

public class App extends Application {

 @Override
 public void onCreate() {
  super.onCreate();

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  if (!prefs.getBoolean("firstTime", false)) {
   // <---- run your one time code here
   databaseSetup();

   // mark first time has runned.
   SharedPreferences.Editor editor = prefs.edit();
   editor.putBoolean("firstTime", true);
   editor.commit();
  }
}

代码示例来源:origin: stackoverflow.com

public class MyActivity extends Activity {

public static final String PREFS_NAME = "MyPrefsFile";

@Override
protected void onCreate(Bundle state){

  super.onCreate(state);
  SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  boolean dialogShown = settings.getBoolean("dialogShown", false);

  if (!dialogShown) {
   // AlertDialog code here

   SharedPreferences.Editor editor = settings.edit();
   editor.putBoolean("dialogShown", true);
   editor.commit();    
  }
}

代码示例来源:origin: rmtheis/android-ocr

void setTorch(Camera camera, boolean newSetting) {
 Camera.Parameters parameters = camera.getParameters();
 doSetTorch(parameters, newSetting);
 camera.setParameters(parameters);
 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
 boolean currentSetting = prefs.getBoolean(PreferencesActivity.KEY_TOGGLE_LIGHT, false);
 if (currentSetting != newSetting) {
  SharedPreferences.Editor editor = prefs.edit();
  editor.putBoolean(PreferencesActivity.KEY_TOGGLE_LIGHT, newSetting);
  editor.commit();
 }
}

代码示例来源:origin: xinghongfei/LookLook

public void show(String tagKey, OnceCallback callback) {
  boolean isSecondTime = mSharedPreferences.getBoolean(tagKey, false);
  if (!isSecondTime) {
    callback.onOnce();
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putBoolean(tagKey, true);
    editor.apply();
  }
}

代码示例来源:origin: stackoverflow.com

Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);
  SharedPreferences.Editor editor = mPrefs.edit();
  editor.putBoolean(welcomeScreenShownPref, true);
  editor.commit(); // Very important to save the preference

代码示例来源:origin: ACRA/acra

public void updateToCurrentVersionIfNecessary() {
    // Check prefs to see if we have converted from legacy (pre 4.8.0) ACRA
    if (!prefs.getBoolean(PREF__LEGACY_ALREADY_CONVERTED_TO_4_8_0, false)) {
      // If not then move reports to approved/unapproved folders and mark as converted.
      new ReportMigrator(context).migrate();

      // Mark as converted.
      prefs.edit().putBoolean(PREF__LEGACY_ALREADY_CONVERTED_TO_4_8_0, true).apply();
    }
    if (!prefs.getBoolean(PREF__LEGACY_ALREADY_CONVERTED_TO_JSON, false)) {
      new ReportConverter(context).convert();

      // Mark as converted.
      prefs.edit().putBoolean(PREF__LEGACY_ALREADY_CONVERTED_TO_JSON, true).apply();
    }
  }
}

代码示例来源:origin: stackoverflow.com

// parse Preference file
SharedPreferences preferences = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);

// get values from Map
preferences.getBoolean("key", defaultValue)
preferences.get..("key", defaultValue)

// you can get all Map but be careful you must not modify the collection returned by this
// method, or alter any of its contents.
Map<String, ?> all = preferences.getAll();

// get Editor object
SharedPreferences.Editor editor = preferences.edit();

//add on Change Listener
preferences.registerOnSharedPreferenceChangeListener(mListener);

//remove on Change Listener
preferences.unregisterOnSharedPreferenceChangeListener(mListener);

// listener example
SharedPreferences.OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener
  = new SharedPreferences.OnSharedPreferenceChangeListener() {
 @Override
 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
 }
};

相关文章