-1

I am new to Android.I am trying access SharedPreferences of one application in another application. But i am not getting those values.

My code was posted below.

Create.java in SharedPref1

package com.example.sharedpref1;

public class Create extends Activity implements OnClickListener{

    EditText et1,et2;
    Button btn;
    String LogID,Pwd;
    public SharedPreferences loginDetails;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create);

        et1 = (EditText)findViewById(R.id.etC1);
        et2 = (EditText)findViewById(R.id.etC2);
        btn = (Button)findViewById(R.id.bCreate);
        loginDetails = getSharedPreferences("logid", MODE_WORLD_READABLE);

        btn.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.bCreate)
        {
            LogID = et1.getText().toString();
            Pwd = et2.getText().toString();
            Toast.makeText(getApplicationContext(), "User Profile Createad With\nUser ID: "+LogID +"\nPassword: "+Pwd, Toast.LENGTH_LONG).show();
            SharedPreferences.Editor store = loginDetails.edit();
            store.putString("logid", LogID);
            store.putString("pass", Pwd);
            store.commit();

            finish();

        }
    }
}

Show.java in SharedPref2

package com.example.sharedpref2;

public class Show  extends Activity implements OnClickListener{

    EditText log,pwd;
    Button back;
    public SharedPreferences loginDetails;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);

        log = (EditText)findViewById(R.id.etid);
        pwd = (EditText)findViewById(R.id.etPwd);
        back = (Button)findViewById(R.id.bBack);

        back.setOnClickListener(this);

        loginDetails = getSharedPreferences("logid", MODE_WORLD_READABLE);

        log.setText(loginDetails.getString("logid", "defValue"));
        pwd.setText(loginDetails.getString("pass", "defValue"));
    }

    }

I am getting values as below enter image description here

KCRaju
  • 27
  • 9

2 Answers2

1

I am trying access SharedPreferences of one application in another application.

This is a bad idea. Quoting the documentation for MODE_MULTI_PROCESS:

SharedPreference loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process. This behavior is sometimes desired in cases where the application has multiple processes, all writing to the same SharedPreferences file. Generally there are better forms of communication between processes, though.

(emphasis added)

Moreover, the only way this could possibly work is if you make the SharedPreferences MODE_WORLD_READABLE, which means any app can get to those preferences. Programmers with talent would not do this, but would use other IPC mechanisms that would limit the communications to between the two applications and only with user permission, so as to not leak user data to others.

Finally, you do not have any code that will work across processes. getSharedPreferences() will get preferences for your own process. The only way I can think of to get a SharedPreferences from another process would require calling getSharedPreferences() on a Context created via createPackageContext(), and I haven't tried this, as I wouldn't dream of implementing what you are proposing.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Please check this http://stackoverflow.com/questions/6030321/android-retrieving-shared-preferences-of-other-application – KCRaju Aug 06 '13 at 17:35
0

If you need to share content between applications I would suggest using a content provider and not SharedPrefrences. In my experience SharedPrefernces is unreliable in these situations.

MODE_WORLD_READABLE is depracated in API level 17. http://developer.android.com/reference/android/content/Context.html#MODE_WORLD_READABLE

Raanan
  • 4,777
  • 27
  • 47