Last active
August 29, 2015 14:01
-
-
Save scottyab/af17da7c45c16930cb1b to your computer and use it in GitHub Desktop.
Code from http://android-developers.blogspot.co.uk/2011/03/identifying-app-installations.html -- For the vast majority of applications, the requirement is to identify a particular installation, not a physical device. Fortunately, doing so is straightforward. There are many good reasons for avoiding the attempt to identify a particular device. Fo…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vf.tools | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.RandomAccessFile; | |
import java.util.UUID; | |
import android.content.Context; | |
public class Installation { | |
private static String sID = null; | |
private static final String INSTALLATION = "INSTALLATION"; | |
/** | |
* Used to Identify App Installations | |
* | |
* @param context | |
* @return | |
*/ | |
public synchronized static String id(Context context) { | |
if (sID == null) { | |
File installation = new File(context.getFilesDir(), INSTALLATION); | |
try { | |
if (!installation.exists()) | |
writeInstallationFile(installation); | |
sID = readInstallationFile(installation); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
return sID; | |
} | |
private static String readInstallationFile(File installation) | |
throws IOException { | |
RandomAccessFile f = new RandomAccessFile(installation, "r"); | |
byte[] bytes = new byte[(int) f.length()]; | |
f.readFully(bytes); | |
f.close(); | |
return new String(bytes); | |
} | |
private static void writeInstallationFile(File installation) | |
throws IOException { | |
FileOutputStream out = new FileOutputStream(installation); | |
String id = UUID.randomUUID().toString(); | |
out.write(id.getBytes()); | |
out.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment