Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 678
Ateeq Avatar asked Jul 25 '13 08:07

Ateeq


People also ask

How does shared preferences work android?

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.

How do we get access to the preference in Android?

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.

How can I get shared 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.

What is the maximum number of shared preferences you can create in Android?

there is no limit in Shared Preference.


2 Answers

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);
like image 164
Ateeq Avatar answered Oct 27 '22 19:10

Ateeq


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();
like image 43
Vipul Avatar answered Oct 27 '22 21:10

Vipul