gpt4 book ai didi

android - 如何在更改首选项时完成工作?

转载 作者:行者123 更新时间:2023-11-29 21:04:34 26 4
gpt4 key购买 nike

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

这是我的 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 添加什么代码才能使我的首选项实际进行更改。

请告诉我。

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

感谢您宝贵的时间

最佳答案

振动

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

关于android - 如何在更改首选项时完成工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24971487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com