2

I need to distinguish my app's installations so I generate a UUID in my app via java.util.UUID.randomUUID() then save it as a String in the shared preferences. However, when I go to the phone's Settings, manage the applications, click on my app, and press "Clear Data," the next time my app runs, a new UUID is generated. I believe this is because the previous shared prefs has been deleted.

Is there a way for me to create a UUID that persists for as long as the app is installed, even when "Clear Data" is pressed from the Settings?

Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132
  • 1
    Why don't you use the phone IMEI code? This way you can identify the device in a unique manner. – Tomas Narros Sep 25 '12 at 09:31
  • 4
    If the device doesn't have 3G or 4G, the IMEI code will practically be non-existent, as explained in http://stackoverflow.com/a/6699237/855680. – Matthew Quiros Sep 25 '12 at 09:34

1 Answers1

4

Clear Data will clear all the files, databases from your app space. One good way to keep persistent data is to create your own private folder in SD card, create a file that saves this UUID. This again isnt foolproof as user may delete the folders from SD card. You can even create invisible folders or invisible files by procceding the filename with a dot.

Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
  • I don't know the Android file system so is there a folder where you'd recommend I write that separate file? – Matthew Quiros Sep 25 '12 at 09:31
  • You can do something like this, File f = new File(Environment.getExternalStorageDirectory() + "/Folder"); f.mkdirs(); This will create a new folder, next you create a file, and write into it. Do u need code for this? i can provide it here – Royston Pinto Sep 25 '12 at 09:35
  • That's tricky, right? Because there might not be an external storage device on the phone or tablet. Maybe internal storage? – Matthew Quiros Sep 25 '12 at 09:37
  • Android always provides a partition named /sdcard/ despite that there might not be an external storage connected. The file system is partitioned such that this space is visible to all app's by using permission WRITE_EXTERNAL_STORAGE – Royston Pinto Sep 25 '12 at 09:39
  • But if the app is uninstalled, will I be able to delete that file or directory? Because if not, the old UUID will be there when the app is reinstalled. There should be a new UUID for every new install. – Matthew Quiros Sep 25 '12 at 10:00
  • 1
    This file will not get deleted during an uninstall. So you will need to figure a way out of determining if it the lastest or not. – Royston Pinto Sep 25 '12 at 10:08
  • Do you have a suggestion on how to determine whether it is the latest? I just tried your suggestion and now I'm back to a dead end. Even if I write a file into the `sdcard` folder, there's still no way for me to react to install or uninstall events so I can't mark the file as new or old. – Matthew Quiros Sep 25 '12 at 12:08
  • Yes i get it, its a really tough one to know when you will be removed and then installed back..you can keep a config file with fields to aid you understand if u need to regenerate a new UUID. But its a tough one i agree – Royston Pinto Sep 25 '12 at 13:55
  • I guess that's the closest I can get to an answer. In any case, I've already begun looking at `AccountManager`. – Matthew Quiros Sep 25 '12 at 14:58