清除android应用程序用户数据


108

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


3
您是否找到任何解决方案来清除外部应用程序的数据?
— 2013年

2
不要以为第三方应用程序应该有权清除另一个应用程序的用户数据。如果那件事发生的话,那将是一团糟。
— StoneLam

Answers:


5

由于Afaik浏览器应用程序数据存储在中,因此无法清除其他应用程序的数据private_mode。因此,执行此命令可能只能在有根设备上起作用。否则,您应该尝试另一种方法。


命令行工具不适合应用程序使用。它们可能仅适用于半特权的adb shell用户,如果使用系统设置应用程序,则在权限上可以与用户在GUI中按按钮相比,在某种程度上可以与之媲美。
— 克里斯·斯特拉顿


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

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

我尝试使用此方法可以清除我们自己的应用程序缓存数据,我需要从我们的应用程序中清除另一个应用程序用户数据
— UdayaLakmal 2012年

2
@UdayaLakmal您不应该能够清除另一个应用程序的缓存数据,因为android旨在保护应用程序免受其他应用程序的潜在恶意操作。
— 克里斯·斯特拉顿

1

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

请检查并告知我...

您可以从这里下载代码


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

1
这将(如果有适当的权限使用)删除外部存储上的共享文件,而不考虑应用程序关联。它不会删除任何应用程序的私人文件,这就是问题所在。
— 克里斯·斯特拉顿

-6

如果您想手动执行操作,则还可以通过单击 应用程序中的“Clear Data” 按钮来清除用户数据Settings–>Applications–>Manage Aplications–>

or Is there any other way to do that?

然后 在这里下载代码

在此处输入图片说明


我要务实地做到这一点,该示例表明您已经尝试过了,但这并不是我想要的
— UdayaLakmal 2012年

如果您尝试过那个,那么请分享您尝试过的那个。好的,让我检查另一种方法。
— Strider 2012年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.