在 android 中取消注册共享首选项更改侦听器的问题



我正在创建一个基本的设置页面,我的代码到目前为止工作正常。但是当我在暂停时添加UnregisterOnSharedPreferenceChangeListener时,它开始出现错误。需要洞察力,我做错了什么以及我应该在哪里插入注销。所有代码如下所示。如果你们能在Android Studio中测试一下,我会很高兴。任何帮助将不胜感激。我试图避免使用已弃用的代码,但如果没有替代方案,我将使用它作为最后的手段。

主要活动.java

public class MainActivity extends AppCompatActivity {
TextView settingsValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button startsettings = (Button)findViewById(R.id.startsettings);
    settingsValue=(TextView)findViewById(R.id.textView);
    startsettings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this,SettingsActivity.class));MainActivity.this.finish();}
    });
}}

设置活动.java

public class SettingsActivity extends PreferenceActivity {
static SharedPreferences sharedPreferences12;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sharedPreferences12 = PreferenceManager.getDefaultSharedPreferences(this);

    getFragmentManager().beginTransaction().replace(android.R.id.content,new SettingsFragment()).commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener
{
    Context context;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.custompreferences);
        context=getActivity();
        Preference prefalarm = findPreference("key_ringtone_alarm");
        Ringtone ringtone = RingtoneManager.getRingtone(getActivity(),Uri.parse(sharedPreferences12.getString("key_ringtone_alarm","")));
        prefalarm.setSummary(ringtone.getTitle(getActivity()));
        Preference prefnoti = findPreference("key_ringtone_notification");
        Ringtone ringtonenoti = RingtoneManager.getRingtone(getActivity(),Uri.parse(sharedPreferences12.getString("key_ringtone_notification","")));
        prefnoti.setSummary(ringtonenoti.getTitle(getActivity()));
        PreferenceManager.getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListener(this);
    }
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Preference pref = findPreference(key);
        if(pref instanceof RingtonePreference)
        {
            Ringtone ringtone = RingtoneManager.getRingtone(context,Uri.parse(sharedPreferences.getString(key,"")));
            pref.setSummary(ringtone.getTitle(context));
        }
    }
}
@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(SettingsActivity.this,MainActivity.class));SettingsActivity.this.finish();
}

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

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.atulk.createcustomsettings.MainActivity">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start Settings"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:id="@+id/startsettings"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintVertical_bias="0.095" />
 </android.support.constraint.ConstraintLayout>

自定义首选项.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:title="User Details"
    android:key="key_user_details">
    <Preference
        android:key="key_user_name"
        android:title="User First Name"
        android:summary="John Smith"/>
    <Preference
        android:key="key_user_emailid"
        android:title="User Email ID"
        android:summary="asd@asd.com"/>
</PreferenceCategory>
<PreferenceCategory
    android:title="Sounds &amp; Notifications"
    android:key="key_sounds_settings">
    <RingtonePreference
        android:key="key_ringtone_alarm"
        android:title="Alarm Ringtone"
        android:ringtoneType="alarm"/>
    <RingtonePreference
        android:key="key_ringtone_notification"
        android:title="Notification"
        android:ringtoneType="notification"/>
    <SwitchPreference
        android:title="Vibrate"
        android:summary="Virate during Alarm or Notification"
        android:key="key_vibrate_onoff"/>
</PreferenceCategory>
<PreferenceCategory
    android:title="Message Delivery"
    android:key="key_message_delivery">
    <SwitchPreference
        android:title="Shorten SMS"
        android:summary="Reduce SMS length by using slangs"
        android:key="key_shorten_sms_length"/>
</PreferenceCategory>
</PreferenceScreen>

只需创建 2 个不同的 java 文件,其中包含首选项活动和首选项片段 并使用getFragmentManager例程从PA调用PF。其余的一切都会好起来的。还可以在 OnDestroy 中使用 UnregisterOnPreferenceChangeListener 而不是 OnPause。

相关内容

  • 没有找到相关文章

最新更新