アプリケーションのインストール元の Android デバイスごとに一意の識別子を取得する方法の詳細については、次の公式の Android 開発者ブログの投稿を参照してください。
http://android-developers.blogspot.com/2011/03/identifying-app-installations.html
インストール時に自分で生成し、アプリケーションの再起動時にそれを読み取るのが最善の方法のようです。
個人的にはこれは許容範囲内ですが、理想的ではありません。Android が提供する 1 つの識別子がすべてのインスタンスで機能するわけではありません。そのほとんどは、電話の無線状態 (wifi のオン/オフ、セルラーのオン/オフ、bluetooth のオン/オフ) に依存するためです。Settings.Secure.ANDROID_ID などの他のものは、製造元が実装する必要があり、一意であるとは限りません。
以下は、アプリケーションがローカルに保存する他のデータと一緒に保存される INSTALLATION ファイルにデータを書き込む例です。
public class Installation {
private static String sID = null;
private static final String INSTALLATION = "INSTALLATION";
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();
}
}