Currently I have created and app to store audio files in sdcard. I will list out all audio files to a ListView, when user click the item and set the notification of date and time for the item, it should appear notify. The question i want to ask is am i need to store this notification (date and time) to the database? How to store it in sdcard?
Asked
Active
Viewed 607 times
0
-
You should just use shared preferences: http://developer.android.com/reference/android/content/SharedPreferences.html It's much easier than implementing and maintaining a database for just one set of values. – Steven Trigg Mar 02 '14 at 02:42
-
Do you have any similar example? I read through the info you given, but i still don't understand how it store the date and time in sharedPreferences. – user3369764 Mar 02 '14 at 02:59
1 Answers
0
The best way to save a collection of dates in Android would be to save them to Shared Preferences. The Shared Preferences uses a Key-Value relationship so you can use the name or ID of your audio file as the key and your date converted to milliseconds as your value.
Here is some example code, I've created a helper class that can load and save to the stored preferences:
public class PreferenceUtil {
public static void saveDateInMillis(Context context, String name, long value) {
SharedPreferences sharedPreferences = context.getSharedPreferences("AppName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong(name, value);
editor.commit();
}
public static long loadDateInMillis(Context context, String name, long defualtValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences("AppName", Context.MODE_PRIVATE);
return sharedPreferences.getLong(name, defualtValue);
}
}
And to use the class:
//Save a calendar object
PreferenceUtil.saveDateInMillis(getActivity(), "audioID", Calendar.getInstance().getTimeInMillis());
//Load a calendar object
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(PreferenceUtil.loadDateInMillis(getActivity(), "audioID", Calendar.getInstance().getTimeInMillis()));
Updated: Just fixed a couple of minor mistakes in the code

Steven Trigg
- 1,903
- 2
- 19
- 22
-
-
It's the name of the file that it creates to store the data, it can be anything you want. Usually the package and/or class names would be appropriate. – Steven Trigg Mar 02 '14 at 22:19
-
I have many audio file in a list, if every audio file have its own reminder time& date, so how i store each of the date in sharedPref? the code above u mention "audioID", but in my files I only detect it by Index of the list, how could i save it? It is possible to store the file name and then compare the file name in the list to get the index? – user3369764 Mar 03 '14 at 02:23
-
It's a unique identifier as a string. As you say it might be best to use the file name as the key, providing it's always unique. – Steven Trigg Mar 03 '14 at 02:39
-
Now i got the idea to store all this details to sharedPreference but now i have no idea on how to get out the value from sharedPreference to match the date and time with the system date and time.? – user3369764 Mar 03 '14 at 02:57
-
I'm not sure what you mean. If you mean you don't know how to set a notification at a specific time, you need to use AlarmManager to start a service that will create the notification. But that's too much to explain in a comment, see: http://stackoverflow.com/a/17099023/213519 – Steven Trigg Mar 03 '14 at 03:13
-
Ok, i'm done the code you given, how i get the "Calendar.getInstance().getTimeInMillis()" from pref and compare to current date&time? – user3369764 Mar 03 '14 at 05:03
-
Use the code given in the answer under "Load a calendar object". You can see that Calendar object cal is created and then set to a saved Date in millis from the shared preferences. The Android Calendar API has lots of functions you can use to compare dates if that's what you're trying to do. – Steven Trigg Mar 03 '14 at 05:13
-
for example i have 5 different date and time in pref, how could i retrieve each of the date and check with current date? Sorry, i new in using sharePreferences... so how the statement i should write? – user3369764 Mar 03 '14 at 05:35
-
You'd need to loop though each of your audio file names, for each one load the millis and create/set a new calendar object. As for the compare I don't know what you want to do. This is getting a bit out of scope of the original question. – Steven Trigg Mar 03 '14 at 05:46