要实际回答您的问题,如果您有根,则可以通过反射来完成。我已经在 android 4.1 上完成了它,在其他平台上它可能会以不同的方式工作。好消息是共享首选项不能仅由系统服务访问,因此您可以通过反射访问“隐藏”方法。把它放在一个try-catch中:
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();