SharedPreferences preferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("myString", "Hello World"); editor.apply();
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); editor.putString("name", "John Doe"); editor.apply();In this example, we get the `SharedPreferences` instance using the `getDefaultSharedPreferences()` method from the `PreferenceManager` class. We then get an editor using the `edit()` method and use the `putString()` method to insert a string value "John Doe" with a key of "name" in the preferences. The package library for `SharedPreferences` is `android.content`.