清除android应用程序用户数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10934304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear android application user data
提问by UdayaLakmal
Using adb shellto clear application data
使用adb shell清除应用程序数据
adb shell pm clear com.android.browser
But when executing that command from application
但是当从应用程序执行该命令时
String deleteCmd = "pm clear com.android.browser";
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (IOException e) {
e.printStackTrace();
}
Issue:
问题:
It doesn't clear the user data neither gives any exception though I have given the following permission.
尽管我已授予以下权限,但它不会清除用户数据,也不会给出任何异常。
<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA"/>
Question:
题:
How to clear the application data using adb shell?
如何使用adb shell清除应用程序数据?
采纳答案by Thkru
Afaik the Browser application data is NOT clearable for other apps, since it is store in private_mode
. So executing this command could probalby only work on rooted devices. Otherwise you should try another approach.
Afaik 浏览器应用程序数据对于其他应用程序是不可清除的,因为它存储在private_mode
. 因此,执行此命令可能仅适用于有 root 权限的设备。否则你应该尝试另一种方法。
回答by Manmohan Soni
This command will only clear the cache:
此命令只会清除缓存:
adb shell pm clear packageName
回答by fantouch
The command pm clear com.android.browser
requires root permission.
So, run su
first.
该命令pm clear com.android.browser
需要 root 权限。
所以,先跑su
。
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
:
班级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();
}
}
}
回答by Md Abdul Gafur
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();
}
回答by Strider
Hello UdayaLakmal,
你好 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
你可以从这里下载代码
回答by My God
// 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();
}
回答by Strider
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
如果您想手动执行,那么您也可以通过单击 您的应用程序中的“Clear Data”
按钮来清除您的用户数据Settings–>Applications–>Manage Aplications–>
or Is there any other way to do that?
Then Download code here
然后 在这里下载代码