I'm just putting the tutorial from the link ihrupin posted here in this post.
package com.hrupin.cleaner;
import java.io.File;
import android.app.Application;
import android.util.Log;
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 cacheDirectory = getCacheDir();
File applicationDirectory = new File(cacheDirectory.getParent());
if (applicationDirectory.exists()) {
String[] fileNames = applicationDirectory.list();
for (String fileName : fileNames) {
if (!fileName.equals("lib")) {
deleteFile(new File(applicationDirectory, fileName));
}
}
}
}
public static boolean deleteFile(File file) {
boolean deletedAll = true;
if (file != null) {
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
}
} else {
deletedAll = file.delete();
}
}
return deletedAll;
}
}
So if you want a button to do this you need to call MyApplication.getInstance(). clearApplicationData() from within an onClickListener
Update:
Your SharedPreferences
instance might hold onto your data and recreate the preferences file after you delete it. So your going to want to get your SharedPreferences
object and
prefs.edit().clear().commit();
Update:
You need to add android:name="your.package.MyApplication"
to the application tag
inside AndroidManifest.xml if you had not done so. Else, MyApplication.getInstance()
returns null
, resulting a NullPointerException.