131

使用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清除应用程序数据?

4

10 回答 10

256

这个命令对我有用:

adb shell pm clear packageName
于 2014-10-16T09:00:44.687 回答
8

Afaik 浏览器应用程序数据对于其他应用程序是不可清除的,因为它存储在private_mode. 所以执行这个命令可能只能在有根设备上工作。否则你应该尝试另一种方法。

于 2012-06-07T15:03:31.573 回答
6

该命令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();
        }
    }
}
于 2014-03-24T07:20:24.817 回答
3

要清除应用程序数据请尝试这种方式。

    public void clearApplicationData() {
    File cache = getCacheDir();
    File appDir = new File(cache.getParent());
    if (appDir.exists()) {
        String[] children = appDir.list();
        for (String s : children) {
            if (!s.equals("lib")) {
                deleteDir(new File(appDir, s));Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
            }
        }
    }
}

public static boolean deleteDir(File dir) {
    if (dir != null &amp;&amp; dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    return dir.delete();
}
于 2012-06-07T15:07:32.220 回答
1

你好乌达亚拉克马尔,

public class MyApplication extends Application {
    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    public static MyApplication getInstance(){
        return instance;
    }

    public void clearApplicationData() {
        File cache = getCacheDir();
        File appDir = new File(cache.getParent());
        if(appDir.exists()){
            String[] children = appDir.list();
            for(String s : children){
                if(!s.equals("lib")){
                    deleteDir(new File(appDir, s));
                    Log.i("TAG", "File /data/data/APP_PACKAGE/" + s +" DELETED");
                }
            }
        }
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    }
}

请检查这个并告诉我...

你可以从这里下载代码

于 2015-05-12T09:58:57.757 回答
0

To clear the cache for all installed apps:

  • use adb shell to get into device shell ..
  • run the following command : cmd package list packages|cut -d":" -f2|while read package ;do pm clear $package;done
于 2021-05-25T15:17:45.513 回答
0

要在 Android 上重置/清除应用程序数据,您需要检查 Android 设备上安装的可用软件包 -

  • adb shell通过在终端上运行进入 adb shell
  • 通过运行检查可用的包pm list packages
  • 如果您想要重置的包名称可用,则pm clear packageName通过将 packageName 替换为您想要重置的包名称来运行,pm list packages结果也会显示相同的内容。

如果未显示包名称,并且您将尝试重置,您将获得失败状态。

于 2021-09-29T09:45:40.010 回答
0

在 Mac 上,您可以使用此命令清除应用数据

adb shell pm clear com.example.healitia

在此处输入图像描述

于 2021-11-26T06:15:27.007 回答
-2
// To delete all the folders and files within folders recursively
File sdDir = new File(sdPath);

if(sdDir.exists())
    deleteRecursive(sdDir);




// Delete any folder on a device if exists
void deleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory())
    for (File child : fileOrDirectory.listFiles())
        deleteRecursive(child);

    fileOrDirectory.delete();
}
于 2014-03-24T07:25:12.580 回答
-6
于 2012-06-07T16:03:29.347 回答