【问题标题】:How to make things done on Preferences being changed?如何在更改首选项时完成任务?
【发布时间】:2014-07-28 16:41:37
【问题描述】:

我已经在我的应用程序中设置了一个 Preference Screen 作为设置选项,但我完全不知道如何在用户选择或更改选项时完成更改。

这是我的 PreferenceScreen 的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="Notifications">
        <RingtonePreference
            android:summary="Choose a Ringtone"
            android:title="Notification Ringtone"
            android:key="ringtonePref" />
        <ListPreference
            android:title="Notification Timer"
            android:summary="Select when to Notify"
            android:dialogTitle="Show Notification after every:"
            android:positiveButtonText="OK"
            android:negativeButtonText="Cancel"
            android:entries="@array/entries"
            android:entryValues="@array/entries"
            android:key="listPrefs" />
        <ListPreference
            android:title="LED Color"
            android:summary="Choose LED Color"
            android:positiveButtonText="OK"
            android:negativeButtonText="Cancel"
            android:entries="@array/colors"
            android:entryValues="@array/colors"
            android:key="listPrefs2" />
        <CheckBoxPreference
            android:title="LED"
            android:defaultValue="true"
            android:summary="Flash LED or Not"
            android:key="checkBoxPrefs2"/>
        <CheckBoxPreference
            android:title="Vibrate"
            android:defaultValue="true"
            android:summary="Vibrate on receiving Notification"
            android:key="checkBoxPrefs3" />
    </PreferenceCategory>

    <PreferenceCategory android:title="Invite">
        <Preference android:summary="Send invitation to more peoples so that\nthey can also Learn more and more Duas."/>
    </PreferenceCategory>

    <PreferenceCategory android:title="Version">
        <Preference
            android:key="versionPrefs"
            android:summary="Version 1.0\nThank you for Downloading the App." />
    </PreferenceCategory>

</PreferenceScreen>

这是我的 Settings.class:

public class Settings extends PreferenceActivity implements OnPreferenceChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

        //setting the properties of Action Bar
        ActionBar actionBar = getActionBar();
        actionBar.setTitle(Html.fromHtml("<font color='#FFFFFF'><strong>Settings</strong></font>"));
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));
        actionBar.setDisplayHomeAsUpEnabled(true);

        PreferenceManager.setDefaultValues(this, R.xml.settings, true);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // TODO Auto-generated method stub
        return false;
    }
}

我不知道应该在 onPreferenceChange 中添加什么代码才能让我的 Preferences 真正进行更改。

请告诉我。

任何帮助都会受到高度赞赏

感谢您的宝贵时间

【问题讨论】:

标签: android settings preferences preferencescreen


【解决方案1】:

振动

CheckBoxPreference checkBoxPrefs3 = (CheckBoxPreference) findPreference("checkBoxPrefs3");
    checkBoxPrefs3.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            if ((Boolean) o) {
                checkBoxPrefs3.setChecked(true); 
                // vibration for 800 milliseconds
                ((Vibrator)getSystemService(VIBRATOR_SERVICE)).vibrate(800);
                // TODO vibrate
            } else {
                checkBoxPrefs3.setChecked(false);
            }
            return false;
        }
    });

在 AndroidManifest.xml 文件中添加所需的权限:

<uses-permission android:name="android.permission.VIBRATE" />

如何申请您的应用?

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean vibrate = sharedPrefs.getBoolean("checkBoxPrefs3", true);

Notification notification = new Notification(icon,
                    "message", when);
            notification.defaults |= Notification.DEFAULT_SOUND;
            if (vibrate) {
                   notification.defaults |= Notification.DEFAULT_VIBRATE;
            }

            notification.setLatestEventInfo(mContext,
                    "ticker",
                    "message", contentIntent);
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(count, notification);

【讨论】:

  • 兄弟,你没明白我的意思,我想将此设置为我的应用程序的设置,当用户检查振动复选框时,设备会振动将收到通知,如果用户取消选中它,设备将不会在收到通知时振动。也请帮助
  • 完成..:) 很好的答案!...非常感谢!
  • 感谢您的回答兄弟,它成功了!现在请告诉我有关 ListPreferences 的信息。我已经在我的问题中发布了,在那里我提供了一个选项,即用户希望在多少小时后获得通知。前 - 每 2 小时或 4 小时后,依此类推。 例如,如果用户选择 2 小时 应用程序应该如何在每 2 小时后通知用户?再次感谢。也请帮忙
  • 在偏好更改中设置闹钟
  • 但是兄弟警报只会设置为特定的时间间隔,我希望它设置为 5 个不同的时间间隔作为用户的选择。怎么做?
【解决方案2】:

关于你的 PreferenceActivity

  • 初始化您的CheckBoxPreference
  • 添加OnPreferenceChangeListener
  • 随心所欲地改变..

这是一个例子:

    CheckBoxPreference checkBoxPrefs3 = (CheckBoxPreference) findPreference("checkBoxPrefs3");
    checkBoxPrefs3.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            if ((Boolean) o) {
                checkBoxPrefs3.setChecked(true); 
                // TODO vibrate
            } else {
                checkBoxPrefs3.setChecked(false);
            }
            return false;
        }
    });

【讨论】:

  • 感谢您的回答,但请说得简单一些。在这里做什么:// TODO vibrate这里应该写什么代码,以便在选中时振动,在未选中时不振动。
  • 请帮忙。
  • 兄弟,你没明白我的意思,我想将此设置为我的应用程序的设置,当用户选中振动复选框时,设备将在收到通知时振动,如果用户取消选中它收到通知时设备不会振动。也请帮忙。
【解决方案3】:

Android 会自动将其保存到您的 SharedPreferences。 您始终可以通过调用获得所需的值:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean led = sharedPrefs.getBoolean("checkBoxPrefs2", true);

checkBoxPrefs2 是布局中定义的键,true 是默认值。

【讨论】:

  • 感谢您的回答。但是我想要一些类似的东西,假设有一个振动选项,如果用户选中它,振动就会发生,如果用户取消选中它,振动就不会发生。请帮助我。
猜你喜欢
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多