-1

I'm working on an app that allow user to scheduled download (user can select multiple links and then at the specific given time (it may take even weeks), AlarmManager will send broadcast to call the DownloadService

So how do I save the selected links of user, even when the app is closed? I think those links just need to be a ArrayList.

I tried SharedPreferences but it only allow me to putInt, putBoolean... I also read about onSaveInstanceState / onRestoreInstanceState but when the app is closed, saveInstanceState is also gone, right?

How can I do this? Thank you very much

4
  • SharedPreferences can store Strings too developer.android.com/reference/android/content/… Commented Jul 1, 2014 at 4:17
  • putExtra(), allows you to store string, did you know about that? Commented Jul 1, 2014 at 4:19
  • Try this link stackoverflow.com/questions/7361627/… Commented Jul 1, 2014 at 4:22
  • I know SharedPreference let me store String, that why I put the "..." after. But if with each url that user selected, I store it with a key in SharedPreference file, this file will be a mess, and I don't want that. Sorry if I didn't clear my intention in my post above. I want to know a way (a good way) to store a lot of url (even 1000) into the app so that when the app closed, the url is still exist. Commented Jul 1, 2014 at 4:59

1 Answer 1

1

If you have an ArrayList<String> urlsList;

Saving URLs :

Editor edit = prefs.edit();
edit.putInt("list_size", urlsList.size());
for(int i=0;i<urlsList.size(); i++)
    edit.putString("url_" + i, urlsList.get(i));
edit.commit();

Retrieving URLs :

int size = prefs.getInt("list_size", 0);
ArrayList<String> retrievedUrls = new ArrayList<String();
for(int i=0; i<size; i++)
    retrievedUrls.add(prefs.getString("url_" + i, null)); 
Sign up to request clarification or add additional context in comments.

4 Comments

The problem with this approach is that you'll pollute your preferences. Say, you save an array with 100 entries and then shrink it ito 2. You'll still have 100 entries in your preferences unless you clean it up first.
Yep sure is. But it can be done. In an ideal situation he should be using the sqlite database.
Can you please clarify the sqlite database solution? Your solution above is get things done but it absolutely make my SharedPreference file a mess. Thank you very much
Just use the SQLite db to save and retrieve URLs. This is an exhaustive tutorial : androidhive.info/2011/11/android-sqlite-database-tutorial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.