简体   繁体   中英

Modify file permissions in Android from code not working

Some time ago I created an application with private Shared Preferences. Now I'm creating a related application that needs to check some of the previous application preferences. The problem I'm facing is that the previous application source code is in a computer I can't access until next month. Since I don't want to wait that long I thought that, since my device is rooted, I might be able to modify the preferences file permissions to be able to access it so I can publish both applications as soon as I get the previous application code back.

The preferences file is located in /data/data/my.package/shared_prefs. If I access it with adb shell and use chmod 777 the file permissions are modified and I can access the preferences, but eventually the permissions will go back to 660. I tried to change them from code using:

Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("chmod 777 /data/data/my.package/shared_prefs/my_preferences.xml");

The permissions aren't modified. Why is that?

Thanks!

My guess is each command run from exec() is executed in a separate native process . Therefore your su command doesn't affect the second call to exec() .

Try this instead:

Runtime.getRuntime().exec("su; chmod 777 /data/data/my.package/shared_prefs/my_preferences.xml");
try {
    Process suProcess = Runtime.getRuntime().exec("su");
    DataOutputStream suOutputStream = new DataOutputStream(suProcess.getOutputStream());
    suOutputStream
            .writeBytes("chmod 777 /data/data/my.package/shared_prefs/my_preferences.xml\n");
    suOutputStream.flush();
    suOutputStream.writeBytes("exit\n");
    suOutputStream.flush();
    suProcess.waitFor();

} catch (IOException e) {
    // TODO Auto-generated catch block
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM