I have a Service in which there are 4 global static int variables and i have a receiver of BOOT COMPLETE and Call event. what I am trying to do is save these 4 variables whenever Call event receiver is execute and Retrieve them when BOOT receiver is executed (of course when I restarted my phone) but Both are not working.. another thing is shared preferences are also useful when device restarts?? the code is given below
SharedPreferences saved_values = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
saved_values.edit().putInt("call", MyService.callcount);
saved_values.edit().putInt("callend",MyService.callendcount);
saved_values.edit().putInt("network",MyService.network_count);
saved_values.edit().putInt("ringing",MyService.ringingcount);
saved_values.edit().commit();
and for retrieving
SharedPreferences saved_values = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
MyService.callcount = saved_values.getInt("call", -10);
MyService.ringingcount=saved_values.getInt("ringing", -10);
MyService. network_count=saved_values.getInt("network", -10);
MyService. callendcount=saved_values.getInt("callend", -10);
A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.
To get access to the preferences, we have three APIs to choose from: getPreferences() : used from within your Activity, to access activity-specific preferences. getSharedPreferences() : used from within your Activity (or other application Context), to access application-level preferences.
These preferences will automatically save to SharedPreferences as the user interacts with them. To retrieve an instance of SharedPreferences that the preference hierarchy in this activity will use, call getDefaultSharedPreferences(android. content. Context) with a context in the same package as this activity.
there is no limit in Shared Preference.
I used this and it worked for me.
For saving
SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor=saved_values.edit();
editor.putInt("count",count);
editor.putInt("foo",foo);
editor.commit();
and for retrieving
SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
count = saved_values.getInt("count", -1);
The problem is each time you call edit() a new Editor object is created.You should hold instance of one Editor object and perform all operations on it.
Use following
SharedPreferences saved_values = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=saved_values.edit();
editor.putInt("call", MyService.callcount);
editor.putInt("callend", MyService.callendcount);
editor.putInt("network", MyService.network_count);
editor.putInt("ringing", MyService.ringingcount);
editor.commit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With