Clear android application user data

后端 未结 7 666
走了就别回头了
走了就别回头了 2020-12-12 11:20

Using adb shell to clear application data

adb shell pm clear com.android.browser

But when executing that command from appl

相关标签:
7条回答
  • 2020-12-12 12:01

    To clear Application Data Please Try this way.

        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();
    }
    
    0 讨论(0)
  • 2020-12-12 12:08

    If you want to do manually then You also can clear your user data by clicking “Clear Data” button in Settings–>Applications–>Manage Aplications–> YOUR APPLICATION

    or Is there any other way to do that?
    

    Then Download code here

    enter image description here

    0 讨论(0)
  • 2020-12-12 12:11
    // 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();
    }
    
    0 讨论(0)
  • 2020-12-12 12:14

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

    Please check this and let me know...

    You can download code from here

    0 讨论(0)
  • 2020-12-12 12:19

    The command pm clear com.android.browser requires root permission.
    So, run su first.

    Here is the sample code:

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

    The class 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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-12 12:22

    This command worked for me:

    adb shell pm clear packageName
    
    0 讨论(0)
提交回复
热议问题