0

I have an alarm manager which I am having difficulty figuring out a way to

A) store a unique ID for each pending intent alarm
B) a name to identify each alarm with and 
C) a number which represents an alarm sound to be used. 

I was using SharedPreferences for this matter but they can only do one instance of an alarm and I would want to store this data for multiple instances of alarms. I need to persist this data even when the app is closed so that when the user opens the app once again, details of the alarms already set can be seen.

alarm1 -> 2431 -> 12
alarm2 -> 8412 -> 42
alarm3 -> 5425 -> 52
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
kabuto178
  • 3,129
  • 3
  • 40
  • 61

1 Answers1

1

You can always use SQLlite database for storing the values in tables. On the other hand if you insist on saving it in Shared Preference, you can use save an array in SharedPreference and Load an array from it as follows:

public String[] loadArray(String arrayName) {  
    SharedPreferences prefs = getSharedPreferences("preferencename", 0);  
    int size = prefs.getInt(arrayName + "_size", 0);  
    String array[] = new String[size];  
    for(int i=0;i<size;i++)  
        array[i] = prefs.getString(arrayName + "_" + i, null);  
    return array;  
}  

public boolean saveArray(String[] array, String arrayName) {   
    SharedPreferences prefs = getSharedPreferences("preferencename", 0);  
    SharedPreferences.Editor editor = prefs.edit();  
    editor.putInt(arrayName +"_size", array.length);  
    for(int i=0;i<array.length;i++)  
        editor.putString(arrayName + "_" + i, array[i]);  
    return editor.commit();  
}

So for saving an array call:

String [] alarmNames; // Load the array with values
saveArray(alarmNames, "nameOfAlarms");

String [] alarmIds; // Load the array with values
saveArray(alarmIds, "idOfAlarms");

String [] alarmSounds; // Load the array with values
saveArray(alarmSounds, "soundOfAlarms");

To load the array from shared preferences

String [] arrName = loadArray("nameOfAlarms");
String [] arrID = loadArray("idOfAlarms");
String [] arrSound = loadArray("soundOfAlarms");

See How to properly use load array and save array methods? and Save ArrayList to SharedPreferences for more.

Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • thanks for the tip, I will look into it and see how I do. In your opinion though, would it be better to use SQLite for this solution? – kabuto178 Jul 27 '13 at 08:12
  • Yes you might just create a table with three columns. I use SQLite for organized data. But on the other hand SharedPreference is faster. – Shobhit Puri Jul 27 '13 at 09:30
  • I tried to implement a method to delete the preferences but I am having a bit of problems doing this – kabuto178 Jul 30 '13 at 00:21
  • If the question is not related to this particular question, then post a new one and comment the link here. I would be more than happy to help you as per my caliber. – Shobhit Puri Jul 31 '13 at 02:31