adb 清除android應用程序用戶數據





4 Answers

命令pm clear com.android.browser需要root權限。
所以,先運行su

以下是示例代碼:

private static final String CHARSET_NAME = "UTF-8";
String cmd = "pm clear com.android.browser";

ProcessBuilder pb = new ProcessBuilder().redirectErrorStream(true).command("su");
Process p = pb.start();

// We must handle the result stream in another Thread first
StreamReader stdoutReader = new StreamReader(p.getInputStream(), CHARSET_NAME);
stdoutReader.start();

out = p.getOutputStream();
out.write((cmd + "\n").getBytes(CHARSET_NAME));
out.write(("exit" + "\n").getBytes(CHARSET_NAME));
out.flush();

p.waitFor();
String result = stdoutReader.getResult();

StreamReader

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.CountDownLatch;

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();
        }
    }
}
android adb user-data

使用adb shell清除應用程序數據

adb shell pm clear com.android.browser

但是從應用程序執行該命令時

String deleteCmd = "pm clear com.android.browser";      
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(deleteCmd);
        } catch (IOException e) {
            e.printStackTrace();                
        }

問題:

雖然我已經給出了以下許可,但並不清楚用戶數據既沒有給出任何例外。

<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA"/>

題:

如何使用adb shell清除應用程序數據?




Afaik瀏覽器應用程序數據對於其他應用程序不可清除,因為它存儲在private_mode 。 所以執行這個命令可能只能在rooted設備上運行。 否則,你應該嘗試另一種方法。




adb卸載com.package.packagename

在ADB外殼上執行此命令。

adb卸載com.package.packagename -k

-k將保留用戶數據




如果您想手動執行操作,則還可以通過單擊Settings–>Applications–>Manage Aplications–>您的應用Settings–>Applications–>Manage Aplications–> “Clear Data”按鈕“Clear Data”用戶數據

or Is there any other way to do that?

然後在這裡下載代碼




Related