I am using getDefaultSharedPreferences(con)
in my application to store preferences. Now I want to access this shared preference in another application.
I used following method:
con = this.createPackageContext("com.example.preferences", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences filePref = PreferenceManager.getDefaultSharedPreferences(con);
if(filePref != null){
Toast.makeText(getApplicationContext(), "file pref not null --- ", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "file pref is null **** ", Toast.LENGTH_LONG).show();
}
Map<String,?> allkeys = filePref.getAll();
Toast.makeText(getApplicationContext(), "file pref size **** "+allkeys.size(), Toast.LENGTH_LONG).show();
for(Map.Entry<String,?> entry : allkeys.entrySet()){
Toast.makeText(getApplicationContext(), "~~~ file pref --- map values --- "+entry.getKey() + ": "+entry.getValue().toString(), Toast.LENGTH_LONG).show();
}
Another way which tried is specifying file name & accessing it, as follow;
SharedPreferences filePref = gvcon.getSharedPreferences("com.example.preferences_preferences", Context.MODE_PRIVATE);
With this method I am able to access SharedPrefernce
file, it returns file is not null but when I check for file size it shows 0. I am not able to read preference value from file.
I used shareduserid
of another application so that I get full access to that application.
What is the proper way to go about this?