SharedPreference的存儲位置:/data/data/{$PACKAGE_NAME}/shared_prefs/
/data/data/{$PACKAGE_NAME}/這裏的文件原本只有應用本身才有權限訪問,可以看到owner和group都是不同的,而且只有所有者才能rw讀寫,連system都無能爲力。這也是從安全角度考量,避免應用數據被惡意篡改。
以下以高德地圖爲例,它的路況播報是默認打開的,修改爲默認關閉:
嘗試一:
使用system uid的應用使用代碼直接拷貝,結果提示無權限:java.io.FileNotFoundException:file open failed:EACCES(Permission denied)
String fileName = "SharedPreferences.xml";
String pathFrom = "/system/media/";
String ptahDest = "/data/data/com.autonavi.minimap/shared_prefs/";
File fileDest = new File(ptahDest + fileName);
if(fileDest.isFile() && fileDest.exists()){
fileDest.delete();
}
if(!fileDest.getParentFile().exists()){
fileDest.getParentFile().mkdirs();
}
// 複製文件
int byteread = 0; // 讀取的字節數
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(new File(pathFrom + fileName));
out = new FileOutputStream(fileDest);
byte[] buffer = new byte[1024];
while ((byteread = in.read(buffer)) != -1) {
out.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
連system uid的應用都提示無權限的話,只能從使用shell腳本修改shared_prefs文件夾所有者和權限的角度入手。
只需要第一次開機執行一次,那麼可以在Provision應用裏觸發。
驗證以後步驟可行:
①拷貝腳本:
#關閉高德地圖路況
busybox mkdir -p /data/data/com.autonavi.minimap/shared_prefs/
busybox cp /system/media/SharedPreferences.xml /data/data/com.autonavi.minimap/shared_prefs/SharedPreferences.xml
busybox chmod 777 -R /data/data/com.autonavi.minimap/shared_prefs/
②腳本放到/system/bin
SharedPreferences.xml放到/system/bin/
③property_service裏初始化觸發值:
{ "app.mapcopy.start", AID_SYSTEM, 0},
④init.*.rc裏聲明服務
service map_copy /system/bin/map_copy
class main
disabled
oneshot
on property:app.mapcopy.start=1
start map_copy
⑤Provision裏設置觸發值爲1,執行腳本:
android.os.SystemProperties.set("app.mapcopy.start","1");
Done!