清理数据需要太多的内存android

时间:2016-01-15 06:50:39

标签: android

我正在尝试使用下面的代码段清除50个浏览器(root)的数据。但是,这个过程太昂贵而且滞后于我的笔记3,有时会在旧设备上崩溃。我如何使这个过程消耗更少的资源?我假设每个人都有大约3个浏览器,但它清除了50个。

private void clearData1() {
    // UC
    String cmd = "pm clear com.UCMobile.intl";
    String cmd1 = "pm clear com.UCMobile";
    String cmd2 = "pm clear com.uc.browser.en";
    String cmd3 = "pm clear com.UCMobile.internet.org";
    String cmd4 = "pm clear uc.ucdl";
    // stock
    String cmd5 = "pm clear com.sec.android.app.sbrowser";
    String cmd6 = "pm clear com.htc.sense.browser";
    String cmd7 = "pm clear com.android.browser";
    // CM
    String cmd8 = "pm clear com.ksmobile.cb";
    // firefox
    String cmd9 = "pm clear org.mozilla.firefox";
    String cmd10 = "pm clear org.mozilla.firefox_beta";

    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));
        out.write((cmd1 + "\n").getBytes(CHARSET_NAME));
        out.write((cmd2 + "\n").getBytes(CHARSET_NAME));
        out.write((cmd3 + "\n").getBytes(CHARSET_NAME));
        out.write((cmd4 + "\n").getBytes(CHARSET_NAME));
        out.write((cmd5 + "\n").getBytes(CHARSET_NAME));
        out.write((cmd6 + "\n").getBytes(CHARSET_NAME));
        out.write((cmd7 + "\n").getBytes(CHARSET_NAME));
        out.write((cmd8 + "\n").getBytes(CHARSET_NAME));
        out.write((cmd9 + "\n").getBytes(CHARSET_NAME));
        out.write((cmd10 + "\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();
}

1 个答案:

答案 0 :(得分:0)

代码段

private class ClearData extends AsyncTask < Void, Void, String > {
    protected String doInBackground(Void...voids) {
        // UC
        String cmd = "pm clear com.UCMobile.intl";
        String cmd1 = "pm clear com.UCMobile";
        String cmd2 = "pm clear com.uc.browser.en";
        String cmd3 = "pm clear com.UCMobile.internet.org";
        String cmd4 = "pm clear uc.ucdl";
        // stock
        String cmd5 = "pm clear com.sec.android.app.sbrowser";
        String cmd6 = "pm clear com.htc.sense.browser";
        String cmd7 = "pm clear com.android.browser";
        // CM
        String cmd8 = "pm clear com.ksmobile.cb";
        // firefox
        String cmd9 = "pm clear org.mozilla.firefox";
        String cmd10 = "pm clear org.mozilla.firefox_beta";
        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));
            out.write((cmd1 + "\n").getBytes(CHARSET_NAME));
            out.write((cmd2 + "\n").getBytes(CHARSET_NAME));
            out.write((cmd3 + "\n").getBytes(CHARSET_NAME));
            out.write((cmd4 + "\n").getBytes(CHARSET_NAME));
            out.write((cmd5 + "\n").getBytes(CHARSET_NAME));
            out.write((cmd6 + "\n").getBytes(CHARSET_NAME));
            out.write((cmd7 + "\n").getBytes(CHARSET_NAME));
            out.write((cmd8 + "\n").getBytes(CHARSET_NAME));
            out.write((cmd9 + "\n").getBytes(CHARSET_NAME));
            out.write((cmd10 + "\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();
        return result;
    }
    protected void onPostExecute(String result) {
        //Do what you want with the result
    }
}

你用这个叫这个子类: new ClearData().execute();