0

My app does different actions deppending on the caller and some predefined values. When a call is starting to ring I need to be as fast as possible to read those preferences from the data base and display them. I tried to have static variables in the BroadcastReceiver, I also tried to have them in a Service but it just seems to die and loose all the values. If I read them from the databade again it takes too long to display the information. So, is there any way to tell Android to respect part of the memory so I can persist the information?

Thanks

EDITED: Is reading from SharedPreferences any faster than reading from a small database?

Ton
  • 9,235
  • 15
  • 59
  • 103
  • depends on how many values plan to store, SharedPreferences are aimed for storing little amount of data (I even read some guys had 50 and they had no problem) and are pretty fast, while SQLite is for storing a larger amount of data. – Marko Niciforovic Apr 05 '13 at 15:29

3 Answers3

1

use SharedPreferences, they are like a private part of you application that will save your values permanently, untill user reinstall (clear data) application. Shared Preferences works like this

// save string in sharedPreferences
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("some_key", string); // here string is the value you want to save
                    editor.commit();                    

// restore string in sharedPreferences 
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
string = settings.getString("some_key", "");

Obviously you can save int boolean etc instead of string

Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
0

No. There's no way to tell Android to persist RAM memory. RAM memory will always be lost when the VM is shutdown.

in the Android developers site there's a good discussion on the options and approaches for permanent storage http://developer.android.com/guide/topics/data/data-storage.html

but for a quick answer is: - depending on the type and amount of data you'll probably want to use the SharedPreferences or a database (SQLite)

Budius
  • 39,391
  • 16
  • 102
  • 144
-1

You can use the Android Shared Preferences. http://developer.android.com/reference/android/content/SharedPreferences.html

The answer of this post is a very great solution for that. Android Shared Preferences

Community
  • 1
  • 1
  • While he should indeed use SharedPreferences, this is a really low quality answer - provide an example, link, or some more explanations. – Shade Apr 05 '13 at 15:10