I am developing an app where I want to allow the user to be able to set up that if they fail the login after a couple of attempts into the app, it will automatically delete all the data including the preferences and databases.
Is there a simple way to do this or do I have to write code to manually reset everything the app uses?
try this code .
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();
}
Is there a simple way to do this or do I have to write code to manually reset everything the app uses.
You have to write code to manually reset everything the app uses. This should just be a matter of deleting a handful of files. Make sure your database is closed before you try deleting it.
Related
Please help with clearing cache in android. I am able to delete cache with below code on android version 6.0. When i am trying it in 7.1 nothing changes. what am i missing?
//method to delete cache- issue reported
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
Log.d("cache clear", "cache delete "+dir);
} catch (Exception e) { e.printStackTrace();}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
Log.d("cache clear", "cache delete 1");
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();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}
From Android API 19 you can do that:
((ActivityManager)context.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();
Official docs:
clearApplicationUserData
added in API level 19
public boolean clearApplicationUserData ()
Permits an application to erase its own data from disk. This is equivalent to the user choosing to clear the app's data from within the device settings UI. It erases all dynamic data associated with the app -- its private data and data in its private area on external storage -- but does not remove the installed application itself, nor any OBB files. It also revokes all runtime permissions that the app has acquired, clears all notifications and removes all Uri grants related to this application.
Returns
boolean true if the application successfully requested that the application's data be erased; false otherwise.
EDIT
From your Activity don't call
deleteCache(this)
but call
deleteCache(getApplicationContext())
Once I open my application it preserves about 7MB (Via Android Studio Memory Monitor), and during using it (Populating a listView with custom items) it pops to about 9MB : 12MB
After I close the application and use 'Initiate GC' from Android Studio it goes down to about 8.3MB.
So, does this mean / indicates that I have a memory leak?
Shouldn't it goes back to 7MB as it started?
or Shouldn't it be 0MB as my application closed?
If you are looking for delete cache of your own application then simply delete your cache directory and its all done !
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
} catch (Exception e) {}
}
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();
}
else if(dir!= null && dir.isFile())
return dir.delete();
else {
return false;
}
}
And you may require following permission to add in your manifest file in order to delete cache of other application
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
Reference : Clear Cache in Android Application programmatically
One day, My customer says "Webview cache seem to stack data area, not cache area. My device is Optimus G 4.4.2 Kitkat".
Whether cache is really accumulate in the data area, i checked my 4.4.2 device.
so, cache was being really accumulated the data area.
and, it does not delete the following way.
protected void trimCache(Context context) {
try {
dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
children = dir.list();
for (String aChildren : children) {
if (deleteDir(new File(dir, aChildren))) continue;
return false;
}
}
assert dir != null;
return dir.delete();
}
and, Webview.clearCache() and Context.deleteDatabase("") .
I have two Question.
How can i delete cache in the data area?
if i can't delete cache in the data area, How i can turn off android webview cache.
p.s. I'm not native speaker, so my english is not enough.
How can i delete cache in the data area?
You can take a look at this link for further info as to how to delete the cache data. There is a recursive method that, that guy is using.
if i can't delete cache in the data area, How i can turn off android webview cache.
You can try using
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setAppCacheEnabled(false);
But more importantly, just after creating your webview, before loading any pages, you can clear the cache, as mentioned in the documentation here.
mWebView.clearCache(true)
i am using this to clear cache from data.
try {
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("databases") &&!s.equals("lib") && !s.equals("shared_prefs")) {
deleteDir(new File(appDir, s));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
I need to simulate Settings -> Clear Data function in my application in just before to deactivate the user account. To do this I use the following code;
private boolean clearCacheData() {
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
boolean result = true;
for (String s : children) {
if (!s.equals("lib")) {
result &= deleteDir(new File(appDir, s));
Log.i("TAG",
"**************** File /data/data/APP_PACKAGE/"
+ s + " DELETED *******************");
}
}
return result;
}
return false;
}
private 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();
}
}
When I do that, I also open my welcome page again in order to give the user a change to reactivate the account. If the user continue to reactivate my app crashes and gives Sq-lite Disk I/O Error. But if user kills the app and then reopen it, everything's just work expected. What might be the problem?
Do I forgot to close something?
It sounds to me that you have forgotten to close either an SQLiteDatabase or a Cursor somewhere in your code. Make sure that you do this before you delete your databases as any future to these will raise an SQLite Disk I/O Error exception.
I managed to delete cached files created by WebView using:
Clearing android cache,
Clear Application cache on exit in android
However for Android 4.4 that solution doesn't work properly, since the files are cached in:
/data/data/com.app.package/app_webview/
instead of:
/data/data/com.app.package/cache/
The above path can be obtained by the official command getCacheDir().
An approach could be hard-coding the path obtained through Get Application Directory
However, is there any [official]/proper solution to address this?
you can use this code
private static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (String aChildren : children) {
boolean success = deleteDir(new File(dir, aChildren));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir != null && dir.delete();
}
void trimCache() {
try {
String pathadmob = this.getFilesDir().getParent() + "/app_webview";
File dir = new File(pathadmob);
if (dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Also, here was generated all admob cache 4.4+, you can use a code to verify how many times the user use the app, and delete the admob cache when the user reached the limit.
Normally, to clear WebView cache, use this WebView API: WebView.clearCache(true);