Getting SharedPreferences from other application in Android
As I wrote inprevious post, where I described how to use preferences system,SharedPreferencesobject can be created and opened withContextmode 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 viaContext. So, to use files from other application you need to use that application’sContext.
I will show 2 simple examples. One of them stores data, other gets data.
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.content.SharedPreferences;
- importandroid.content.SharedPreferences.Editor;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- /**
- *Classwhichshowshowtostorepreferences
- *
- *@authorFaYnaSoftLabs
- */
- publicclassMainextendsActivity{
- publicstaticfinalStringPREFS_PRIVATE="PREFS_PRIVATE";
- publicstaticfinalStringKEY_PRIVATE="KEY_PRIVATE";
- publicstaticfinalStringPREFS_READ="PREFS_READ";
- publicstaticfinalStringKEY_READ="KEY_READ";
- publicstaticfinalStringPREFS_WRITE="PREFS_WRITE";
- publicstaticfinalStringKEY_WRITE="KEY_WRITE";
- publicstaticfinalStringPREFS_READ_WRITE="PREFS_READ_WRITE";
- publicstaticfinalStringKEY_READ_WRITE="KEY_READ_WRITE";
- privateSharedPreferencessharedPreferences;
- privateEditTextprivateField;
- privateButtonprivateBtn;
- privateEditTextreadField;
- privateButtonreadBtn;
- privateEditTextwriteField;
- privateButtonwriteBtn;
- privateEditTextread_writeField;
- privateButtonread_writeBtn;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- privateField=(EditText)findViewById(R.id.privateId);
- privateBtn=(Button)findViewById(R.id.privateBtn);
- privateBtn.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- sharedPreferences=getSharedPreferences(PREFS_PRIVATE,Context.MODE_PRIVATE);
- EditorprefsPrivateEditor=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(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- sharedPreferences=getSharedPreferences(PREFS_READ,Context.MODE_WORLD_READABLE);
- EditorprefsPrivateEditor=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(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- sharedPreferences=getSharedPreferences(PREFS_WRITE,Context.MODE_WORLD_WRITEABLE);
- EditorprefsPrivateEditor=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(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- sharedPreferences=getSharedPreferences(PREFS_READ_WRITE,Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
- EditorprefsPrivateEditor=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’sContext:
- ContextotherAppsContext=createPackageContext("other.application.package",mode);
This method creates other application’sContext, you only need to know package name. And after you can get preferences usingotherAppsContext.
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 creatingContextfor 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 thatPRIVATEandWORLD_WRITABLEaren’t available.PRIVATEis ok, but whyWORLD_WRITABLE?
It is Linux file system and as you can see on the imageWORLD_WRITABLE(PREFS_WRITE) doesn’t have read permission forothergroup.
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.
本文介绍如何在Android中从一个应用程序访问另一个应用程序的SharedPreferences。通过使用Context的不同模式,可以实现不同权限级别的数据共享。
215

被折叠的 条评论
为什么被折叠?



