1

Implementing the User's chosen Preferences into my Code is a new conquest for me.
I've successfully implemented a portion of the Preferences to Code, but could use advice.

I have chosen to use the Preferences-API and a PreferenceFragment for my App Settings.
So far I have my SettingsActivity set up, working, and updating new values correctly.
However, some assistance is needed now that I'm implementing Preference values to Code.

There are 2 Preferences which I am struggling to implement into Code.
However, for now, I'll discuss just 1 of them.. The details are as follows :

Preference = Notification Type
( key = pref_notificationType ) - ( constant String = PREF_NOTIFICATION_TYPE )

Values :
Sound and Vibration
Sound only
Vibration only
Silent

( NOTE : These are the exact value names for this Preference ).


I want to do something along these lines, except correctly, and efficiently:

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


If anybody could please provide some advice on how to go about this, I would greatly appreciate it!

Thanks in advance!

Studio2bDesigns
  • 578
  • 5
  • 12

1 Answers1

1

I think in this case the best way create Enum and compare this in 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:
}
Thiago Neves
  • 91
  • 1
  • 7
  • @Thiago_Neves: Thank you for the reply! I am not yet familiar with using `enum`, or exactly what it's function is (I'll Google it in a moment) - however, it seems that in *your Example*, the `switch` statement is roughly the same as in *my Example* (except without the `break;`'s, and adding the `enum` portion).. Could you possibly give me a brief explanation of how *your Example* is different than *mine*, and how the `enum` portion of it plays into changing things/making it different, compared to simply using the `switch` statement alone? Either way wouldn't I add my case code(s) all the same? – Studio2bDesigns Feb 09 '19 at 00:42
  • PS. After researching `enum`, both on *Google* as well as *StackOverlow*, I've noticed a common theme that suggests the use of `enum` is actually **discouraged**, and that the use of *Primitive Constants* are recommended instead, **OR**, using `TypeDef` Annotations instead.. **References:** [link on StackOverflow](https://stackoverflow.com/questions/9246934/working-with-enums-in-android) and [link on Google search](https://android.jlelse.eu/android-performance-avoid-using-enum-on-android-326be0794dc3).. Any thoughts? or additional suggestions? – Studio2bDesigns Feb 09 '19 at 00:51