如何标识android device?

使用唯一ID是必须的,那么有哪些可选的ID呢?

http://android-developers.blogspot.com/2011/03/identifying-app-installations.html提供了一些参考。

  • TelephonyManager.getDeviceId()
    可以用来标识每1台Android Phone,非Phone不可以用这个方法
  • Mac Address
    对于有wifi/bluetooth的设备,可以用它们的mac address来标识
  • Android_ID
    即Settings.Secure.Android_ID,据说是当设备第一次启动时系统自动产生的,是个64bit的数字
  • 自定义UUID
  • 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();
        }
    }

Advertisements