清除Android应用程序用户数据

时间:2012-06-07 14:55:02

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

9 个答案:

答案 0 :(得分:172)

这个命令对我有用:

adb shell pm clear packageName

答案 1 :(得分:5)

命令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();
        }
    }
}

答案 2 :(得分:4)

Afaik浏览器应用程序数据不能清除其他应用程序,因为它存储在private_mode中。因此,执行此命令可能只适用于root设备。否则你应该尝试另一种方法。

答案 3 :(得分: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();
}

答案 4 :(得分:1)

Hello UdayaLakmal,

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();
    }
}

请检查一下并告诉我......

您可以从here

下载代码

答案 5 :(得分:0)

清除所有已安装应用的缓存:

  • 使用 adb shell 进入设备外壳 ..
  • 运行以下命令:cmd package list packages|cut -d":" -f2|while read package ;do pm clear $package;done

答案 6 :(得分:-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();
}

答案 7 :(得分:-2)

adb uninstall com.package.packagename

在ADB shell上执行此命令。

adb uninstall com.package.packagename -k

-k将保留用户数据

答案 8 :(得分:-4)

如果您想手动执行,那么您也可以点击“Clear Data”您的申请中的Settings–>Applications–>Manage Aplications–>按钮来清除用户数据

or Is there any other way to do that?

然后 Download code here

enter image description here