Ateeq
Ateeq

Reputation: 520

Android Shared Preferences not working

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);

Upvotes: 14

Views: 27729

Answers (3)

Farruh Habibullaev
Farruh Habibullaev

Reputation: 2392

After seeing how people are struggling with shared preference management such as editing, retrieving and whether it is default or custom, I have created a open source library for very easy save and retrieve of shared preference with one line of code.

public class SimplePrefExample extends Activity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    new SimplePrefs.Builder()
            .setPrefsName("myapppreference")
            .setContext(this)
            .setMode(MODE_PRIVATE)
            .setDefaultUse(false)
            .build();

}

} After creating easy preference, you can edit and retrieve the data so easily.

public class SimplePrefExample extends Activity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SimplePrefs.putInt("myid", 384);
    SimplePrefs.putString("username", "smash");
}

}

This library solved many issues such as checking for availablity of preference and etc.

You can get details here in the link https://github.com/farruhha/SimplePrefs

Upvotes: 0

Ateeq
Ateeq

Reputation: 520

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);

Upvotes: 26

Vipul
Vipul

Reputation: 28093

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();

Upvotes: 14

Related Questions