如何保存应用程序的数据?

时间:2016-03-10 11:25:25

标签: android su

我已经编写了一个Web View应用程序,它会将您登录到12个不同的站点(登录),这些站点非常好用。但是,我试图找到一种方法来备份我的网络视图的数据(以便保存所有登录凭据)到SD卡。我找到的唯一方法是复制root/data/data/com.example/your app文件夹。

如何在点击按钮时使用root命令将此文件夹复制到我的SD卡?

这是我访问和删除数据文件夹的方式

private void clear() {
    String cmd = "pm clear com.wagtailapp";
    ProcessBuilder pb = new ProcessBuilder().redirectErrorStream(true)
            .command("su");
    Process p = null;
    try {
        p = pb.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
    StreamReader stdoutReader = new StreamReader(p.getInputStream(),
            CHARSET_NAME);
    stdoutReader.start();
    out = p.getOutputStream();
    try {
        out.write((cmd + "\n").getBytes(CHARSET_NAME));
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    try {
        out.write(("exit" + "\n").getBytes(CHARSET_NAME));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        p.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    String result = stdoutReader.getResult();
}

}

streamreader.java

class StreamReader extends Thread {
private InputStream is;
private StringBuffer mBuffer;
private String mCharset;
private CountDownLatch mCountDownLatch;

StreamReader(InputStream is, String charset) {
    this.is = is;
    mCharset = charset;
    mBuffer = new StringBuffer("");
    mCountDownLatch = new CountDownLatch(1);
}

String getResult() {
    try {
        mCountDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return mBuffer.toString();
}

@Override
public void run() {
    InputStreamReader isr = null;
    try {
        isr = new InputStreamReader(is, mCharset);
        int c = -1;
        while ((c = isr.read()) != -1) {
            mBuffer.append((char) c);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (isr != null)
                isr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mCountDownLatch.countDown();
    }
}

}

0 个答案:

没有答案