I am trying to allow setting different preferences for contacts that are saved in db via PreferenceScreen
. For that I read that in order to create multiple SharedPreferences
, one can do something like:
SharedPreferences prefContact1 = getSharedPreferences("micheal", Context.MODE_PRIVATE);
SharedPreferences prefContact2 = getSharedPreferences("john", Context.MODE_PRIVATE);
SharedPreferences prefContact3 = getSharedPreferences("frady", Context.MODE_PRIVATE);
// and so on
And so I am creating different preferences like above here:
// create contact-based pref
String phone = cursor.getString(cursor.getColumnIndexOrThrow("phone"));
user_preference = getSharedPreferences(phone, Context.MODE_PRIVATE);
// start contact pref screen / activity
Intent intent = new Intent(this, ContactSettingActivity.class);
intent.putExtra("phone", phone);
startActivity(intent);
And On ContactSettingActivity
, I have this code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String phone = intent.getStringExtra("phone");
this.addPreferencesFromResource(R.xml.user_preferences);
this.initSummaries(this.getPreferenceScreen());
// get contact's own pref
SharedPreferences prefs = getSharedPreferences(phone, MODE_PRIVATE);
prefs.registerOnSharedPreferenceChangeListener(this);
// ... more code
Here is my user_preferences.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Notification Settings" >
<RingtonePreference
android:defaultValue="content://settings/system/notification_sound"
android:key="user_prefRingtone"
android:ringtoneType="notification"
android:showDefault="true"
android:showSilent="true"
android:summary="Default ringtone for incoming SMS"
android:title="SMS Ringtone" />
<CheckBoxPreference
android:defaultValue="true"
android:key="user_prefVibrate"
android:summary="Vibrate on incoming SMS"
android:title="Vibrate" >
</CheckBoxPreference>
<ListPreference
android:defaultValue="0"
android:entries="@array/pref_notif_icon_entries"
android:entryValues="@array/pref_notif_icon_values"
android:key="user_prefnotificon"
android:summary="Change the notification area icon"
android:title="Notification Icon" />
<ListPreference
android:defaultValue="Off"
android:entries="@array/pref_led_color_entries"
android:entryValues="@array/pref_led_color_values"
android:key="user_prefled"
android:summary="Color of the LED"
android:title="LED Color" />
<CheckBoxPreference
android:defaultValue="true"
android:key="user_prefWakeup"
android:summary="Wakeup screen on incoming SMS"
android:title="Screen Wakup" >
</CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
Now problem is that even though I am setting different preferences based on contact's phone via getSharedPreferences(phone, Context.MODE_PRIVATE)
but still when I go to any user's preference screen, it is always the same settings applied to all contacts :(
Can anyone suggest what I am doing wrong or how to set different preferences for items (contacts) of ListView so that each one will have different notification sound, notification icon etc for example ? Or any other approach that works is also welcome :)
Thanks for your help :)
Ok I found it, I added below line to settings activity:
this.getPreferenceManager().setSharedPreferencesName(phone);
here is updated code:
public class ContactSettingActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String phone = intent.getStringExtra("phone");
SharedPreferences prefs = getSharedPreferences(phone, MODE_PRIVATE);
this.getPreferenceManager().setSharedPreferencesName(phone);
this.addPreferencesFromResource(R.xml.user_preferences);
this.initSummaries(this.getPreferenceScreen());
prefs.registerOnSharedPreferenceChangeListener(this);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.