我尝试允许为通过PreferenceScreen
保存在db中的联系人设置不同的首选项。为此,我读到为了创建多个SharedPreferences
,可以执行以下操作:
SharedPreferences prefContact1 = getSharedPreferences("micheal", Context.MODE_PRIVATE);
SharedPreferences prefContact2 = getSharedPreferences("john", Context.MODE_PRIVATE);
SharedPreferences prefContact3 = getSharedPreferences("frady", Context.MODE_PRIVATE);
// and so on
所以我在这里创建了不同的偏好:
// 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);
在ContactSettingActivity
上,我有这段代码:
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
这是我的user_preferences.xml
文件:
<?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>
现在的问题是,即使我通过getSharedPreferences(phone, Context.MODE_PRIVATE)
根据联系人的电话设置不同的偏好设置,但是当我转到任何用户的偏好设置屏幕时,它始终与所有联系人应用的设置相同:(
任何人都可以建议我做错了什么或如何为ListView的项目(联系人)设置不同的首选项,以便每个人都会有不同的通知声音,通知图标等?或任何其他有效的方法也欢迎:)
感谢您的帮助:)
答案 0 :(得分:0)
好的我找到了,我在下面添加了一行设置活动:
this.getPreferenceManager().setSharedPreferencesName(phone);
这里是更新的代码:
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);
}