0

I am trying to get the SharedPreferenes that I have saved in two different activities into my SMS activity. They are both saved as Strings.

For the phone number, it is saved as a string and is just directly inside of the onActivityResult (because it is getting the phone number out of the contact list)

prefs = getSharedPreferences(prefName, MODE_PRIVATE);
                        SharedPreferences.Editor editor = prefs.edit();

                        //---save the values in the EditText view to preferences---
                        editor.putString("phoneNumber", phn_no);

                        //---saves the values---
                       editor.commit();

                        phoneN.setText(phn_no);
                        Toast.makeText(getBaseContext(), "Saved",
                                Toast.LENGTH_SHORT).show();

For the message, it has a method.

public void AddMessage() {
    btnSave.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    prefs = getSharedPreferences(prefName, MODE_PRIVATE);
                    SharedPreferences.Editor editor = prefs.edit();

                    //---save the values in the EditText view to preferences---
                    //editor.putInt("id", Integer.parseInt(e_id.getText().toString()));
                    editor.putString("message", editMessage.getText().toString());

                    //---saves the values---
                    editor.commit();

                    Toast.makeText(getBaseContext(), "Saved",
                            Toast.LENGTH_SHORT).show();
                }
            }

    );
}

I have tried doing this:

SharedPreferences settings =    PreferenceManager.getDefaultSharedPreferences(this);
                        String phoneNum = settings.getString("phoneNumber", "");
                        String message = settings.getString("message", "");
                        Log.d(TAG, message.toString());
                        Log.d(TAG, phoneNum.toString());

But it is not saved in this. Might have did it wrong...

Thanks in advance for your help!

EDIT

also tried to do this:

SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(this);
                        if (spref.contains("message")) {
                            String mes = spref.getString("message", "");
                            Log.d(TAG, mes)
;                            }
                        if(spref.contains("phoneNumber")){
                            String phn = spref.getString("phoneN", "");
                            Log.d(TAG,phn);
                        }

it did not work. Nothing has showed up in the Log.

isabella
  • 151
  • 4
  • 15
  • What are you getting? – Nabin May 01 '15 at 04:13
  • make a change last section to context.getSharedPreferences(prefName, MODE_PRIVATE); – Shijil May 01 '15 at 04:15
  • See: http://stackoverflow.com/questions/5946135/difference-between-getdefaultsharedpreferences-and-getsharedpreferences – Kage May 01 '15 at 04:17
  • Nabin - I am getting Strings that I have already saved in the SharedPreferences. shijil - what is "context" – isabella May 01 '15 at 04:20
  • @isabella http://stackoverflow.com/questions/3572463/what-is-context-in-android – Shijil May 01 '15 at 04:31
  • Please update your code with the changes you've made, and put the log results in the question as well if you're still having problems. – Daniel Nugent May 01 '15 at 04:33
  • @shijil oh okay you meant this "Context.getSharedPreferences(prefName, MODE_PRIVATE);" but it also didn't work the error it gave was " Non-Static method 'getSharedPreferences()' cannot be referenced from static context – isabella May 01 '15 at 04:48
  • That's not what he meant, when you call `getSharedPreferences()` it's the same thing as calling `this.getSharedPreferences()`, where `this` is the context. It looks like you are still writing to different prefs than you are reading from. – Daniel Nugent May 01 '15 at 05:01
  • Because I am trying to understand how to get the prefs. I have them saved in two different activities. I was trying to call all the prefs from one of the activities but that didn't work. Should I just save the all the prefs in each activity in theSharedPreferences spref ? – isabella May 01 '15 at 05:05

4 Answers4

1

Instead of this line

SharedPreferences settings =    PreferenceManager.getDefaultSharedPreferences(this);

use

SharedPreferences settings = getSharedPreferences(prefName, MODE_PRIVATE);
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
1

you want to store something like .... in Preferences. so do like first to create Common class like..

package com.ibl.commonClass;

import android.content.Context;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.graphics.Bitmap;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.preference.PreferenceManager;

import android.view.View;


public class Global {

public SharedPreferences SharedPref;
Editor Editor;
Context context;
public Global(Context context) {
    this.context = context;
    SharedPref = this.context.getSharedPreferences("PREFS_NAME",
            Context.MODE_PRIVATE);

}

public static void setPreferenceString(Context c, String pref, String val) {
    Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
    e.putString(pref, val);
    e.commit();
}

public static void setPreferenceInt(Context c, String pref, int val) {
    Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
    e.putInt(pref, val);
    e.commit();
}

public static void setPreferenceLong(Context c, String pref, Long val) {
    Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
    e.putLong(pref, val);
    e.commit();
}

public static void setPreferenceBoolean(Context c, String pref, Boolean val) {
    Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
    e.putBoolean(pref, val);
    e.commit();
}

public static void clearPreferenceUid(Context c, String pref) {
    Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
    e.remove(pref);
    e.commit();
}

public static boolean getPreferenceBoolean(Context c, String pref,
        Boolean val) {
    return PreferenceManager.getDefaultSharedPreferences(c).getBoolean(
            pref, val);
}

public static int getPreferenceInt(Context c, String pref, int val) {
    return PreferenceManager.getDefaultSharedPreferences(c).getInt(pref,
            val);
}

public static Long getPreferenceLong(Context c, String pref, Long val) {
    return PreferenceManager.getDefaultSharedPreferences(c).getLong(pref,
            val);
}

public static String getPreferenceString(Context c, String pref, String val) {
    return PreferenceManager.getDefaultSharedPreferences(c).getString(pref,
            val);
}

}

now you use any activity to set preferences like

 String mEmail;

this i get when user login in application

 mEmail = obj.getString("email");
Global.setPreferenceString(getApplicationContext(), "email",mEmail);

and like this you get Preferences

 Global.getPreferenceString(getActivity(), "email", "");

in Activity to use "getApplicationContext()" and in fragment "getActivity()" Thanks & Regards

Developer
  • 6,292
  • 19
  • 55
  • 115
Hardik Parmar
  • 712
  • 2
  • 13
  • 28
0

Use this while accesing sharedprefrence istead of

PreferenceManager.getDefaultSharedPreferences(this);

// Use this

SharedPreferences settings = getSharedPreferences(prefName, MODE_PRIVATE);
Rishi Paul
  • 936
  • 2
  • 8
  • 25
0

Seems you are missing something, I don't know what but try do keep the SharedPreference name in a constant file and make it final. Because it is possible that you save the values in a different SharedPreference and get from different. For example:--

/*
**Declare SharedPreference name as a final
*/
public static final String PREFS_NAME = "MY_PREFS";

Now use this variable to get a SharedPreferences from anywhere. For example:--

SharedPreferences prefs = context.getSharedPreference(PREFS_NAME, MODE_PRIVATE);
Paresh P.
  • 6,677
  • 1
  • 14
  • 26