-1

am making a app that clears the internet cache when you click a button. In which i want it to have the permission you clear other app data. I have tried many different codes and cant find a one that is used for this purpose. Is there a special permission i need or is it even possible.

I have tried this code in MainActivity.java but cant seem to get it to work:

private void clearPreferences() {
try {
    // clearing app data
    Runtime runtime = Runtime.getRuntime();
    runtime.exec("pm clear com.android.browser");

} catch (Exception e) {
    e.printStackTrace();
}

}

Any help would be great. Thanks!

1 Answers1

1

To clear it you simply delete the contents of it's data directory. There are 2 locations one on the SD, and another internally. You can clear the one on SD no problem, just use Apache FileUtils from Apache Commons, it has a function to delete a directory recursively. The internal dir requires root access. Or to be of same user. If you made both apps then it should be no problem just make sure they are the same user by setting sharedUserId parameter. If not, then your app could only work as root. See this answer to get data directory.

An alternative to rooting, you can open up the app info page for the app in question and tell the user to clear the app data manually by tapping on the clear data & clear cache buttons.

Community
  • 1
  • 1
over_optimistic
  • 1,399
  • 2
  • 18
  • 27
  • Thank you for your reply, i have heard of some apps that allow you to clear cache on browser, apps, etc. I am guessing that those use the Apache IOUtils? If so where could i find the Apache IOUtils function(Im new to android developing). – user3696320 Jun 02 '14 at 00:13
  • I linkified. Sorry I meant the Apache FileUtils (not IO). They probably do use it, it's a very common library with very useful functions in the library to do super common tasks. Like moving files, joining streams, converting Files/Streams to string and much more. I would guess the cache will default to SD since there is more space available on SD and is publicly accessible by all processes. – over_optimistic Jun 02 '14 at 01:26
  • looks like this has been flagged as duplicate. Check the duplicate. this one http://stackoverflow.com/a/23470355/884893 looks interesting – over_optimistic Jun 02 '14 at 01:32