Getting SharedPreferences from other application in Android

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;

/**
 * Class which shows how to store preferences
 *
 * @author FaYnaSoft Labs
 */
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;

	@Override
	public 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() {

			@Override
			public 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() {

			@Override
			public 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() {

			@Override
			public 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() {

			@Override
			public 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;

/**
 * Class which shows how to get preferences from other application
 * @author FaYnaSoft Labs
 *
 */
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;

	@Override
	public 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() {

			@Override
			public 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?
file_system
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.

Share

8 comments to Getting SharedPreferences from other application in Android

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.