您的位置:首页 > 移动开发 > Android开发

Android 覆盖修改第三方APP SharedPreference里存储的配置

2016-04-29 10:38 991 查看
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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: