在root应用程序的Android应用程序中获取root权限?

时间:2012-08-09 12:56:17

标签: android android-file

如何获取将资产文件夹中的文件复制到/ etc的应用程序的root权限或权限以及通常在非root设备上无法访问的其他文件夹。

我编写了以下代码,但事实证明它不会产生任何错误,也不会执行所需的操作。 我是否需要在清单中编辑某些内容?

public class CopyFilesFromApkToSystemActivity extends Activity {
    /** Called when the activity is first created. */

    private static final String FILE1 = "usb_modeswitch.conf";
    private static final String FILE2 = "usb_modeswitch";
    private static final String FILE3 = "gprs";
    private static final String FILE4 = "ip-up";
    private static final String FILE5 = "cdma.sh";

    private static String PATH1 = "/etc/";
    private static String PATH2 = "/system/xbin/";
    private static String PATH3 = "/etc/ppp/peers/";
    private static String PATH4 = "/etc/ppp/";
    private static String PATH5 = "/system/xbin/";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Toast.makeText(this, "Copy started", Toast.LENGTH_SHORT).show();

        try {
            copyfile(FILE1,PATH1);
            copyfile(FILE2,PATH2);
            copyfile(FILE3,PATH3);
            copyfile(FILE4,PATH4);
            copyfile(FILE5,PATH5);



        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




    }

    private void copyfile(String fileName, String pathName) throws IOException {

        InputStream mInput = this.getAssets().open(fileName);
        String outFileName = fileName + pathName;
        OutputStream mOutput = new FileOutputStream(outFileName);
        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer))>0)
        {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();

        //Toast.makeText(this, fileName + " copied", Toast.LENGTH_SHORT).show();


    }
}

2 个答案:

答案 0 :(得分:0)

有人写了一个特定的类来获得root权限,你可以检查一下: http://muzikant-android.blogspot.ca/2011/02/how-to-get-root-access-and-execute.html

答案 1 :(得分:0)

可能是文件夹权限问题。在开始复制并尝试

之前,请设置文件夹权限
try {
                Runtime.getRuntime().exec("chmod 777 /etc/");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }