0

I have two app let suppose "AppOne" and "AppTwo",In AppOne I have some value stored in its shared Prefrence like "String "name",What I want to get this value from "appTwo".How can I do that

code of AppOne Sahred Preference:-

private SharedPreferences m_Preference;
private SharedPreferences.Editor m_Editor;
private final String MY_PREF="AppData";

public PreferenceHelper(Context context){
    this.m_Preference = context.getSharedPreferences(MY_PREF,Context.MODE_PRIVATE);
    this.m_Editor = m_Preference.edit();
}
/*Saving String value......*/
public void saveStringPreference(String key,String value){
    m_Editor.putString(key,value);
    m_Editor.apply();
}
public String getStringPreference(String key){
    return m_Preference.getString(key,"");
}

/*Saving int value........*/
public void saveIntegerValue(String key,int value){
    m_Editor.putInt(key,value);
    m_Editor.apply();
}
public int getIntPreference(String key){
    return m_Preference.getInt(key,1);
}

And in MainActivity I save that value:-

preferenceHelper = new PreferenceHelper(getApplicationContext());

    preferenceHelper.saveStringPreference("Name", "ABC");
Nitin
  • 163
  • 2
  • 11

2 Answers2

0

You can not do that. SharedPreferences are local for the package of the app and can not be accessed directly from the outside (security reasons, most likely).

If you want other apps to be able to get some data from your app (SharedPrefs or any other data) you will need to define a ContentProvider, an BroadcastReceiver, Service or an external (web) api :-)

Kelevandos
  • 7,024
  • 2
  • 29
  • 46
0

Why don't you write a file instead of using sharedPreferences to store some data. With this way, you can reach the data from another android application.

To write a txt file

public void writeToFile(String data)
{
    // Get the directory for the user's public pictures directory.
    final File path =
        Environment.getExternalStoragePublicDirectory
        (
            //Environment.DIRECTORY_PICTURES
            Environment.DIRECTORY_DCIM + "/YourFolder/"
        );

    // Make sure the path directory exists.
    if(!path.exists())
    {
        // Make it, if it doesn't exit
        path.mkdirs();
    }

    final File file = new File(path, "myText.txt");

    // Save your stream, don't forget to flush() it before closing it.

    try
    {
        file.createNewFile();
        FileOutputStream fOut = new FileOutputStream(file);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.append(data);

        myOutWriter.close();

        fOut.flush();
        fOut.close();
    }
    catch (IOException e)
    {
        Log.e("Exception", "File write failed: " + e.toString());
    } 
}

to read the file :

public String readTheFile(){
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"myText.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
    br.close();
}
catch (IOException e) {
    //You'll need to add proper error handling here
}
 return text.toString();
}
ziLk
  • 3,120
  • 21
  • 45
  • This is a great solution, but you assume that the OP is looking for a way to so this and not for a 'hack' to access the data of other apps – Kelevandos Sep 30 '16 at 11:50
  • @Kelevandos, It depands on what type of data should I store in Android. I like to use to sharedprefrences or writting a file to store small strings etc. Wrinting/reading the file is much easier than using contentProvider in this case. – ziLk Sep 30 '16 at 12:05
  • Yes, you are right, but only if you are the developer of both apps that you want to communicate – Kelevandos Sep 30 '16 at 12:12