As I wrote in previous post,where I described how to use preferences system,SharedPreferences object can be created and opened with Context mode constants. These constants allows to define who has access to what preference. Also it is not a secret that data contains in xml file. As I said constants allows to define access to preferences and it is not a problem to have access from other application to these files. I am going to show example how you can access preferences from other application. There is one thing which you need to understand. You create and open preferences via Context. So,to use files from other application you need to use that application’s Context.
I will show 2 simple examples. One of them stores data,other gets data.
import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class Main extends Activity{public static final String PREFS_PRIVATE = "PREFS_PRIVATE";public static final String KEY_PRIVATE = "KEY_PRIVATE";public static final String PREFS_READ = "PREFS_READ";public static final String KEY_READ = "KEY_READ";public static final String PREFS_WRITE = "PREFS_WRITE";public static final String KEY_WRITE = "KEY_WRITE";public static final String PREFS_READ_WRITE = "PREFS_READ_WRITE";public static final String KEY_READ_WRITE = "KEY_READ_WRITE";private SharedPreferences sharedPreferences;private EditText privateField;private Button privateBtn;private EditText readField;private Button readBtn;private EditText writeField;private Button writeBtn;private EditText read_writeField;private Button read_writeBtn;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);privateField = (EditText) findViewById(R.id.privateId);privateBtn = (Button) findViewById(R.id.privateBtn);privateBtn.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){sharedPreferences = getSharedPreferences(PREFS_PRIVATE,Context.MODE_PRIVATE);Editor prefsPrivateEditor = sharedPreferences.edit();prefsPrivateEditor.putString(KEY_PRIVATE,privateField.getText().toString());prefsPrivateEditor.commit();privateField.setText("")}});readField = (EditText) findViewById(R.id.readId);readBtn = (Button) findViewById(R.id.readBtn);readBtn.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){sharedPreferences = getSharedPreferences(PREFS_READ,Context.MODE_WORLD_READABLE);Editor prefsPrivateEditor = sharedPreferences.edit();prefsPrivateEditor.putString(KEY_READ,readField.getText().toString());prefsPrivateEditor.commit();readField.setText("")}});writeField = (EditText) findViewById(R.id.writeId);writeBtn = (Button) findViewById(R.id.writeBtn);writeBtn.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){sharedPreferences = getSharedPreferences(PREFS_WRITE,Context.MODE_WORLD_WRITEABLE);Editor prefsPrivateEditor = sharedPreferences.edit();prefsPrivateEditor.putString(KEY_WRITE,writeField.getText().toString());prefsPrivateEditor.commit();writeField.setText("")}});read_writeField = (EditText) findViewById(R.id.read_writeId);read_writeBtn = (Button) findViewById(R.id.read_writeBtn);read_writeBtn.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){sharedPreferences = getSharedPreferences(PREFS_READ_WRITE,Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);Editor prefsPrivateEditor = sharedPreferences.edit();prefsPrivateEditor.putString(KEY_READ_WRITE,read_writeField.getText().toString());prefsPrivateEditor.commit();read_writeField.setText("")}})}}As you can see it is simple example which stores preferences with several permission modes.
Now I am going to show how you can access other application’s Context:
Context otherAppsContext = createPackageContext("other.application.package",mode);This method creates other application’s Context,you only need to know package name. And after you can get preferences using otherAppsContext.
import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.pm.PackageManager.NameNotFoundException;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class Main2 extends Activity{public static final String PREFS_PRIVATE = "PREFS_PRIVATE";public static final String KEY_PRIVATE = "KEY_PRIVATE";public static final String PREFS_READ = "PREFS_READ";public static final String KEY_READ = "KEY_READ";public static final String PREFS_WRITE = "PREFS_WRITE";public static final String KEY_WRITE = "KEY_WRITE";public static final String PREFS_READ_WRITE = "PREFS_READ_WRITE";public static final String KEY_READ_WRITE = "KEY_READ_WRITE";private SharedPreferences sharedPreferences;private EditText privateId;private EditText read;private EditText write;private EditText readWrite;private Button btn;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);privateId = (EditText) findViewById(R.id.privateId);read = (EditText) findViewById(R.id.read);write = (EditText) findViewById(R.id.write);readWrite = (EditText) findViewById(R.id.read_write);btn = (Button) findViewById(R.id.btn);btn.requestFocus();btn.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){Context otherAppsContext = null;try{otherAppsContext = createPackageContext("com.faynasoftlabs.tutorial.android.sharedpreferences",0)} catch (NameNotFoundException e){}sharedPreferences = otherAppsContext.getSharedPreferences(PREFS_PRIVATE,Context.MODE_PRIVATE);privateId.setText(sharedPreferences.getString(KEY_PRIVATE,"PRIVATE EMPTY"));sharedPreferences = null;sharedPreferences = otherAppsContext.getSharedPreferences(PREFS_READ,Context.MODE_WORLD_WRITEABLE);read.setText(sharedPreferences.getString(KEY_READ,"WORLD READ EMPTY"));sharedPreferences = null;sharedPreferences = otherAppsContext.getSharedPreferences(PREFS_WRITE,Context.MODE_WORLD_WRITEABLE);write.setText(sharedPreferences.getString(KEY_WRITE,"WORLD WRITE EMPTY"));sharedPreferences = null;sharedPreferences = otherAppsContext.getSharedPreferences(PREFS_READ_WRITE,Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);readWrite.setText(sharedPreferences.getString(KEY_READ_WRITE,"WORLD READ WRITE EMPTY"));sharedPreferences = null}})}}As you can see I used
otherAppsContext = createPackageContext("com.faynasoftlabs.tutorial.android.sharedpreferences",0);for creating Context for my other application. And after that I got preferences from that application.
Also you should understand that you can’t get all preferences. When you will receive results you will see that PRIVATE and WORLD_WRITABLE aren’t available. PRIVATE is ok,but why WORLD_WRITABLE?
It is Linux file system and as you can see on the image WORLD_WRITABLE (PREFS_WRITE) doesn’t have read permission for other group.
In my point of view it is very great solution when you have several installed applications,and you want to share some simple data without building some protocols for communication.
But you can use it not only in this case.
I hope that it was useful article for you.
Download code from this article.
No related posts.

Thanks for this good example.
One remark:
You use the flag Context.MODE_WORLD_WRITEABLE in createPackageContext. I think this is not correct.
The documenattion says “Option flags,one of CONTEXT_INCLUDE_CODE or CONTEXT_IGNORE_SECURITY.”The second one maps to the same number so you are using that by accident. Actually,I think no flag is needed at all here (0).
Hello Matthias.
. But with your help it looks correct now.
Thank you very much for your attention.
Yes,you are right. I updated my code.
I can’t recollect why I put this mode
Thanks,it works! BTW,MODE_PRIVATE would work if both apps use the same SharedUserId in the manifest and sign with the same key. I was just able to verify this on Android 1.6. This way your data is still kept private from other apps.
Hi David.
I knew it,but I didn’t try this way with SharedUserId.
Thanks for this information.
[...] Getting SharedPreferences from other application in Android [...]
Thanks a lot.
Thanks or the info
Very nice Tutorial !! it was very helpful.Thank you.