I'm working on cache cleaner app, after doing research on google I found that Android system has moved "CLEAR_APP_CACHE" permission to "signature, privileged" state. So I'm unable clear cache with freeStorageAndNotify method.
Apps on google playstore like CCleaner, Power Clean etc.. are using Accessibility Service To Delete Cache.
I have also created basic accessibility service for my app, but don't know how to delete cache of apps
You can get a list of installed apps and delete cache like:
public static void clearALLCache()
{
List<PackageInfo> packList = getPackageManager().getInstalledPackages(0);
for (int i=0; i < packList.size(); i++)
{
PackageInfo packInfo = packList.get(i);
if ( (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)
{
String appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
try {
// clearing app data
// Runtime runtime = Runtime.getRuntime();
// runtime.exec("pm clear "+packInfo.packageName);
Context context = getApplicationContext().createPackageContext(packInfo.packageName,Context.CONTEXT_IGNORE_SECURITY);
deleteCache(context);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
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;
}
}
It is equivalent to clear data option under Settings --> Application Manager --> Your App --> Clear data. (In comment)
For making your application support this you should also follow Privileged Permission Whitelisting from google
Related
i need to clear cache of an app without force closing the app. i need app to continue running. i am using espresso to test application but i need to clear cache before the app starts. is there any possible way to do it ?
public static void clearPreferences(Activity activity) {
try {
// clearing app data
String packageName = activity.getPackageName();
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear "+packageName);
} catch (Exception e) {
e.printStackTrace();
}
}
this is what i have. but it closes the app and terminates test case
check this link below it's helpful to you.
Clear Cache in Android Application programmatically
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;
}
}
add permission in manifest
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
I am developing a application need to clean specific application cache data, like device setting clear app cache button, I already find out the clear data solution, but how can I find out that application cache data directory so that I can remove that cache folder?
Big Thanks.
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"/>
I have an Android app with intent filter for "http". After install the user get "open with" dialog when he press a url in the browser.
My question is: If the user chose by mistake "open with x always" and he wants to change his selection to open the url with another app, how can he get the "choose app" dialog again ?
You have to clear the cache of the application.
For clearing cache you have take permission -
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
Call deleteCahce() to clear cahe
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;
}
}
Clear the cache before showing "Open With".
Is there some way to programmatically clear the cache of an android app when upgrading to a new version or during an installation?
android have clearApplicationUserData() in ActivityManager Class.
this method will have the same effect as explicit hit on clear app data in settings do.
here's the reference--
http://developer.android.com/reference/android/app/ActivityManager.html#clearApplicationUserData()
hope this helps
cheers!
Write receiver with action "PACKAGE_REPLACED" in manifest.
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
// write logic to delete cache directory
// Get cache dir as context.getCacheDir()
}
}
Revering to this answer you can clear the app's cache the follwing way:
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {}
}
private 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();
}
Simply call deleteCache() everytime your app is updated.
To check whether you app is updated or not you can simply save the version string into a SharedPreference and compare the saved value with the actual one.
public static boolean isNewVersion(Context context) {
String newVersion = "", oldVersion = "";
PackageManager manager = context.getPackageManager();
PackageInfo info = null;
try {
info = manager.getPackageInfo(context.getPackageName(), 0);
newVersion = info.versionName;
} catch (PackageManager.NameNotFoundException e) {
// TODO what to do?
}
oldVersion = getVersionString();
// returns whether the new version string differs (is also false for first install)
return !(oldVersion.equals(DEFVAL_STRING) || oldVersion.equals(newVersion));
}
public static String getVersionString(Context context) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return preferences.getString(KEY_VERSION, DEFVAL_STRING);
}
If you just want this for a webview, you can add cache busting to your urls loaded by the webview. Add the app version to the querystring and you'll get a fresh download after users upgrade:
"http://www.stackoverflow.com?v=" + BuildConfig.VERSION_NAME
I am writing an application where i m planning to store the Images read from server to Android cache. As I read somewhere "Android cache can be cleared by system whenever the memory is low", so if I image is removed how will i get the indication that the image is removed?
I think there is no way to know wether Android has cleared the cache. But if you save data to the cache folder you can hold on to the filename and check wether the file is present or not.
I have a download task for my data which checks the cache first and if there is no data present the download starts. Otherwise the cached file is used.
For clearing app cache put this code in onDestroy()
protected void onDestroy() { super.onDestroy();
try {
trimCache(this);
// Toast.makeText(this,"onDestroy " ,Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void trimCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
// TODO: handle exception
}
}
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;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}