如何使事情在preferences做被改变? [英] How to make things done on Preferences being changed?

查看:220
本文介绍了如何使事情在preferences做被改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了preference屏幕在我的应用程序设置选项,但我完全糊涂了如何当用户选择或更改一个选项使所做的更改。

I've set up a Preference Screen as a settings option in my App, but I am totally confused about how to make the changes done when user selects or changes an Option.

这里是我的preferenceScreen的XML code:

<?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;
    }
}

我不知道是什么code,我应该增加在preferenceChange让我的preferences实际上做的改变。

请告诉我。

任何帮助将是非常 鸭preciated

Any help would be highly Appreciated.

感谢您的 precious时间

推荐答案

有关振动

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);

这篇关于如何使事情在preferences做被改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆