Android modify others shared preference

這次想要透過更新之後,去修改別的APP的shared preference。最後有修改成功,但是以結果來說是失敗的,懷疑目標APP是把功能做在屬性onchange的時候才會trigger,而不是依照preference讀出來再做設定。

在這次收尋過程當中,有找到特定APP能處理一樣的事情Cheat Droid ★ root only

Android: Modify SharedPreferences of another appstackoverflow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "chmod 777 /data/data/package_name/shared_prefs/package_name_preferences.xml" });
proc.waitFor();
proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "chmod 777 /data/data/package_name/shared_prefs" });
proc.waitFor();
proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "chmod 777 /data/data/package_name" });
proc.waitFor();

File preffile = new File("/data/data/package_name/shared_prefs/package_name_preferences.xml");

Class prefimplclass = Class.forName("android.app.SharedPreferencesImpl");

Constructor prefimplconstructor = prefimplclass.getDeclaredConstructor(File.class,int.class);
prefimplconstructor.setAccessible(true);

Object prefimpl = prefimplconstructor.newInstance(preffile,Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);


Editor editor = (Editor) prefimplclass.getMethod("edit").invoke(prefimpl);
//put your settings here
editor.commit();

How can I share a SharedPreferences file across two different android apps?