将用户选择的偏好实现到我的代码中对我来说是一个新的征服。
我已经成功实现了代码偏好的部分,但可以使用建议。
我已选择使用 Preferences-API
和 PreferenceFragment
作为我的 App
设置。
到目前为止,我的 SettingsActivity
已正确设置、工作和更新新值。
但是,现在我正在对代码实现偏好值,因此需要一些帮助。
有 2 Preferences
我正在努力将其实现到代码中。
但是,现在,我将只讨论其中的1。详细信息如下:
Preference = Notification Type
(key = pref_notificationType)-(constant String = PREF_NOTIFICATION_TYPE)
值(value)观:
声音和振动
只有声音
仅振动
沉默
(注意:这些是此首选项
的确切值名称)。
我想按照这些思路做一些事情,但要正确且高效:
public void notificationType() {
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
final String notifType =
getPrefs.getString(PREF_NOTIFICATION_TYPE, "Sound and Vibration");
switch (notifType) {
case ("Sound and Vibration"):
// Create Notification with SOUND (AND) VIBRATION
break;
case ("Sound only"):
// Create Notification with SOUND (only)
break;
case ("Vibrate only"):
// Create Notification with VIBRATION (only)
break;
case ("Silent"):
// Create Notification SILENTLY
break;
default:
// The default is "Sound and Vibration"
break;
}
}
如果有人可以就如何解决这个问题提供一些建议,我将不胜感激!
提前致谢!
最佳答案
我认为在这种情况下,最好的方法是创建 Enum 并在 swithc 中进行比较。
enum NotificationType {
SOUND_AND_VIBRATE, SOUND_ONLY, VIBRATE_ONLY, SILENT
}
//and
switch (NotificationType) {
case SOUND_AND_VIBRATE:
case SOUND_ONLY:
case VIBRATE_ONLY:
case SILENT:
}
关于Android - SharedPreferences - 根据偏好值调整代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54600352/