Android: Retrieving shared preferences of other ap

2018-12-31 20:46发布

I have a settings application from which i have to retrieve other applications preferences, but i don't have the details of keys in them, how can i retrieve all the available keys and values in that preference?

Thanks, Swathi

6条回答
墨雨无痕
2楼-- · 2018-12-31 20:54

It's simple to retrieve store shared preferences data of one application to another application.

Step 1: add the same android:sharedUserId="android.uid.shared" in both app's manifest files.

Step 2: Store Value application1

 SharedPreferences preferences = context.getSharedPreferences("token_id",    Context.MODE_WORLD_READABLE);
        Editor editor = preferences.edit();
        editor.putString("shared_token", encryptedValue);
        Log.e("aaa *** shared_token : ", encryptedValue.toString());
        editor.commit();

Step 3: Get Value From application2

Context con = null;
        try {
            con = createPackageContext("application2 package name", Context.CONTEXT_IGNORE_SECURITY);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        try {
            if (con != null) {
                SharedPreferences pref = con.getSharedPreferences(
                        "token_id", Context.MODE_WORLD_READABLE);

                String data = pref.getString("shared_token", "");
                Log.d("msg", "Other App Data: " + data);
            } else {
                Log.d("msg", "Other App Data: Context null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
查看更多
人间绝色
3楼-- · 2018-12-31 20:55

It can work if we want read perference value from other app/pkg/process. but there is something wrong in jkhouw1's answer:

Context myContext = createPackageContext("com.example", 
            Context.MODE_WORLD_WRITEABLE);

It should be :

Context myContext = createPackageContext("com.example", 
            Context.CONTEXT_IGNORE_SECURITY);

though , CONTEXT_IGNORE_SECURITY and MODE_WORLD_WRITEABLE with the same value of "int 2" At all ,thanks for this question and answers.

查看更多
刘海飞了
4楼-- · 2018-12-31 20:57

Unfortunately the docs now don't even explain MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE, instead saying:

This constant was depreciated in API level 17. Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, ....etc

Since the depreciation, implementing file sharing between apps with sharedpreferences may be too risky, although it was simple. I'm not too concerned with security holes from the MODE_WORLD_READABLE mode in game apps where I just want to be able to transfer characters from one app to another. It's too bad they depreciated both sharing modes.

查看更多
梦寄多情
5楼-- · 2018-12-31 20:58

Additionally you have to add same android:sharedUserId in the both app's manifest file.

查看更多
冷夜・残月
6楼-- · 2018-12-31 21:10

Assuming the preference are WORLD_READABLE, this might work:

final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
Context myContext = createPackageContext("com.example", 
Context.MODE_WORLD_WRITEABLE); // where com.example is the owning  app containing the preferences
  SharedPreferences testPrefs = myContext.getSharedPreferences 
("test_prefs", Context.MODE_WORLD_READABLE); 


Map<String, ?> items = testPrefs .getAll();
for(String s : items.keySet()){
    //do somthing like String value=  items.get(s).toString());
    }
查看更多
浮光初槿花落
7楼-- · 2018-12-31 21:14

Okay! using this code in Application 1 ( with package name is "com.sharedpref1" ) to store data with Shared Preferences.

SharedPreferences prefs = getSharedPreferences("demopref",
                    Context.MODE_WORLD_READABLE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("demostring", strShareValue);
            editor.commit();

And using this code in Application 2 to get data from Shared Preferences in Application 1. We can get it because we use MODE_WORLD_READABLE in application 1:

    try {
                con = createPackageContext("com.sharedpref1", 0);
                SharedPreferences pref = con.getSharedPreferences(
                        "demopref", Context.MODE_PRIVATE);
                String data = pref.getString("demostring", "No Value");
                displaySharedValue.setText(data);

            } catch (NameNotFoundException e) {
                Log.e("Not data shared", e.toString());
            }

More information please visit this URL: http://androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html

查看更多
登录 后发表回答