0

I need to be able to clear the internal storage directory of my application when the user uninstalls my app. How can I do this?

I am in the testing phase of building a security app which requires an ID to be set by an admin when the application is first installed to a user's phone. The ID file cannot be in external storage because I do not want any tampering.

In the event that an administrator gives a user the incorrect ID, they will not be able to fix this by uninstalling the app right now. I have tried uninstalling the app (and clearing cache, etc) to clear the ID data. Upon reinstalling the application, the files still exist, so the ID number cannot be changed.

I am saving the file to the main internal files directory for my application, if that helps.

IDFile = new File(context.getFilesDir(),"ID_Data.txt");
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

It's not possible to remove the internal storage, an app doesn't get a callback when uninstalled, so there's nothing it can do to wrap things up.

But it should work for external storage: Use getExternalFilesDir() and/or getExternalCacheDir() for your files on external storage ("sdcard"). Those directories are automatically removed when your app is uninstalled. (source)

You can consider another approach - encrypt the files that the app stores with a key stored only in your code, so decryption will be possible only by the app, and when uninstalled - the remaining files will remain encrypted.

Idea #2

Delete data of older installation, if re-installed

  1. Upon launch of your app, check if there's a preference "first_run", and if it's empty - create one with current time.
  2. If there is already such a preference, you need to check if it's from this installation of from an old one: check the actual install time.
  3. If your preference is smaller than the install time: delete all the old files.
Community
  • 1
  • 1
Amir Uval
  • 14,425
  • 4
  • 50
  • 74
  • I was thinking that there might be something you could place in the manifest that tells the system to remove everything. It's not exactly a callback, but more like a flag. I might have to make a workaround... –  Jul 16 '17 at 10:38
  • I'm not aware of such. But I have another idea for you - I'll add it to my answer – Amir Uval Jul 16 '17 at 12:04
  • Idea #2 is similar to what I was thinking of doing myself, but was missing the idea of the time stamp. That will do the trick. You also answered the main question in your first 3 words. I typically try to make use of developer functions that already exist for operating system level operations, so I'm quite surprised that the developers have not included an option for clearing data upon uninstalling apps... Thanks for the assist. –  Jul 16 '17 at 23:08
  • Found something that may help: Use getExternalFilesDir() and/or getExternalCacheDir() for your files on external storage ("sdcard"). Those directories are automatically removed when your app is uninstalled. – Amir Uval Jul 16 '17 at 23:17
  • I'll keep that in mind, and appreciate your time! Thanks again! –  Jul 16 '17 at 23:28
0

In the manifest specify AutoBackup to "false"

android:allowBackup="false"

https://developer.android.com/guide/topics/data/autobackup.html

Mark Sheekey
  • 608
  • 7
  • 23
  • Thanks for the reply, but I don't think this will work. I bought a tablet today and tested my app, uninstalled, and then reinstalled. The data was still there upon reinstalling... In other words, the problem persists without a google account for backing up the data. Unless, of course, there is some storage online for those without google accounts. –  Jul 16 '17 at 10:35