Can I get data from shared preferences inside a service?

AndroidSharedpreferencesAndroid ServiceAndroid 2.2-Froyo

Android Problem Overview


I'm developing an android application. I'm using android 2.2

In my application I am capturing GPS data and sending it to service with the 1 hour time interval. If user exits from application it's also working (it is required).

I'm using 2 services (User defined), one for capturing GPS data and other for sending to the server.

Here my doubt

  • In service, can we use shared preferences.

  • If we store any data in shared preferences in any activity of the application, will we be able to use that data in service with the help of shared preferences?

Android Solutions


Solution 1 - Android

You can access the default shared preferences instance, which is shared across all your Activity and Service classes, by calling PreferenceManager.getDefaultSharedPreferences(Context context):

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

This is great for storing simple primitives (like booleans) or serializable objects. However, if you're capturing a lot of location data, you might consider using a SQLite database instead.

Solution 2 - Android


I find the solution.
Inside a service we call the following method to get the shared preferences

myapp.bmodel.getApplicationContext().getSharedPreferences("myPrefs_capture_gps_per_hour", Context.MODE_PRIVATE);


In the above code myapp is a object of the application class which is derived from Application

Solution 3 - Android

You need a context to get access to shared preferences. The best way is to create MyApplication as a descendant of Application class, instantiate there the preferences and use them in the rest of your application as MyApplication.preferences:

public class MyApplication extends Application {
	public static SharedPreferences preferences;

	@Override
	public void onCreate() {
		super.onCreate();

		preferences = getSharedPreferences( getPackageName() + "_preferences", MODE_PRIVATE);

For example, if you need access to your preferences somewhere else, you may call this to read preferences:

String str = MyApplication.preferences.getString( KEY, DEFAULT );

Or you may call this to save something to the preferences:

MyApplication.preferences.edit().putString( KEY, VALUE ).commit();

(don't forget to call commit() after adding or changing preferences!)

Solution 4 - Android

Yes Shivkumar, you can use your share preferences in any kind of services as normal as you are using in your Activity.

same like

SharedPreferences preferences = getSharedPreferences("<PrefName>",
			MODE_PRIVATE);

Solution 5 - Android

There are two ways to create instance of SharedPreference:

Case 1:

SharedPreferences preferences = activity.getSharedPreferences("<PrefName>", MODE_PRIVATE);

Case 2:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

Notice if you create a preference with the same name (case 1) or same context (case 2) even at different places, it's still the same, and can share data, obviously.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSIVAKUMAR.JView Question on Stackoverflow
Solution 1 - AndroidtwaddingtonView Answer on Stackoverflow
Solution 2 - AndroidSIVAKUMAR.JView Answer on Stackoverflow
Solution 3 - AndroidlenikView Answer on Stackoverflow
Solution 4 - AndroidJignesh AnsodariyaView Answer on Stackoverflow
Solution 5 - AndroidNguyen Tan DatView Answer on Stackoverflow