Pertanyaan Hapus data pengguna aplikasi android


Menggunakan adb shell untuk menghapus data aplikasi

adb shell pm clear com.android.browser

Tetapi ketika mengeksekusi perintah itu dari aplikasi

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

Isu:

Tidak jelas data pengguna tidak memberikan pengecualian apa pun meskipun saya telah memberikan izin berikut.

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

Pertanyaan:

Cara menghapus data aplikasi menggunakan adb shell?


76
2018-06-07 14:55


asal


Jawaban:


Perintah ini berhasil untuk saya:

adb shell pm clear packageName

140
2017-10-16 09:00



Perintah pm clear com.android.browser membutuhkan izin root.
Jadi, larilah su pertama.

Berikut ini contoh kode:

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

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

5
2018-03-24 07:20



Untuk mengosongkan Data Aplikasi, Silakan Coba dengan cara ini.

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

2
2018-06-07 15:07



Afaik data aplikasi Peramban TIDAK dapat dihapus untuk aplikasi lain, karena disimpan di dalamnya private_mode. Jadi mengeksekusi perintah ini bisa probalby hanya bekerja pada perangkat yang di-rooting. Kalau tidak, Anda harus mencoba pendekatan lain.


1
2018-06-07 15:03



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

Silakan periksa ini dan beri tahu saya ...

Anda dapat mengunduh kode dari sini


0
2018-05-12 09:58



adb uninstall com.package.packagename

jalankan perintah ini di shell ADB.

adb uninstall com.package.packagename -k 

-k akan mempertahankan data pengguna


-1
2018-02-08 16:12



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

-2
2018-03-24 07:25



Jika Anda ingin melakukan secara manual maka Anda juga dapat menghapus data pengguna Anda dengan mengklik “Clear Data”  tombol di Settings–>Applications–>Manage Aplications–>  APLIKASI ANDA

or Is there any other way to do that?

Kemudian Unduh kode di sini

enter image description here


-4
2018-06-07 16:03