minesh..
minesh..

Reputation: 155

How to use sharedPreference value in different classes in android

in main Activity i have following:

((TextView)findViewById(R.id.TextView02)).setText(getSharedPreferences("FearAlert", 1).getString("contactName", "Tap to select an Emergency Contact."));
    ((TextView)findViewById(R.id.TextView03)).setText(getSharedPreferences("FearAlert", 1).getString("contactNumber", ""));

now i want to use contactNumber value in ANOTHER Activty say activity2 in Activty2 is:

 SmsManager.getDefault().sendTextMessage(??, null, "message",null, null, null);
return null;

what should i write in place of ?? above..help please..

Upvotes: 0

Views: 964

Answers (4)

baconcheese113
baconcheese113

Reputation: 963

Assuming you have created the SharedPreferences correctly:

SharedPreferences settings = getSharedPreferences("MY_APP", MODE_PRIVATE);

You can retrieve a preference in your method by:

SmsManager.getDefault().sendTextMessage(settings.getString("contactNumber"), null, "message", null, null, null);

Assuming you've set it with the "MY_APP" shared preference tag.

settings.edit().putString("contactNumber", "XXX-XXX-XXXX").commit();

Upvotes: 0

alex
alex

Reputation: 5694

you can access you preferences everywhere in your app e.g. with following method:

/**
 * Get a string preference by its key from the apps preferences file
 * 
 * @param key of preference
 * @return value of preference
 */
public String getStringPreference(String key) {
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    return pref.getString(key, "");
}

Just set your key, e.g. "contactName" as parameter. The preferences manager requires the application context via getApplicationContext().

Upvotes: 0

Ramesh Sangili
Ramesh Sangili

Reputation: 1631

Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity (which is not discussed here).

The context object lets you retrieve SharedPreferences through the method

Context.getSharedPreferences(). 


getSharedPreferences("FearAlert", 1).getString("contactNumber", "");

OR 

 SharedPreferences myPrefs = this.getSharedPreferences("FearAlert", 1);
        String contactNumber = myPrefs.getString(contactNumber, "nothing");

Upvotes: 0

wtsang02
wtsang02

Reputation: 18873

In Java, there are... Never mind, In programming languages, there are variables where you can assign values to variables. like x=1; then use can use x+x=?; and will give you 2;

Here you can do

SharedPreferences sPrefs=getSharedPreferences("FearAlert", 1);
TextView tv=(TextView)findViewById(R.id.TextView02);
String YOUR_INTRESTING_STRING=sPrefs.getString("contactName", "Tap to select an Emergency Contact.");

And to pass it to the another Activity; You can put it in a Bundle

Intent i = new Intent(getApplicationContext(), YOUR_ANOTHER_ACTIVITY.class);
i.putExtra("name_of_value",YOUR_INTRESTING_STRING);
startActivity(i);


And retrive it in your AnotherActivity by

Bundle extras = getIntent().getExtras();
String value = extras.getString("name_of_value");

Upvotes: 1

Related Questions