Android唯一ID

jai*_*min 9 android

如何从Android手机获取唯一ID?

每当我尝试从手机中获取唯一ID作为字符串时,它总是显示android id并且没有其他唯一的十六进制值.

我怎么得到那个?

这是我用来获取ID的代码:

String id=Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);
Log.i("Android is is:",id);
Run Code Online (Sandbox Code Playgroud)

我得到的输出看起来像这样:

Android id is: android id
Run Code Online (Sandbox Code Playgroud)

我正在使用Nexus One进行测试.

Kev*_*ker 17

有关如何为安装应用程序的每个Android设备获取唯一标识符的详细说明,请参阅此官方Android开发人员博客帖子:

http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

看起来最好的方法是在安装时生成一个自己,然后在重新启动应用程序时读取它.

我个人觉得这个可以接受但不理想.Android提供的任何一个标识符都不适用于所有情况,因为大多数都依赖于手机的无线电状态(wifi开/关,蜂窝开/关,蓝牙开/关).其他像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();
    }
}
Run Code Online (Sandbox Code Playgroud)


dra*_*ard 4

((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
Run Code Online (Sandbox Code Playgroud)

与清单

<uses-permission android:name='android.permission.READ_PHONE_STATE' />
Run Code Online (Sandbox Code Playgroud)

编辑:

这里有一些关于 android id 的有趣读物:

如何设置安卓ID

Android ID 需要市场登录

尝试将其设置为“android id”以外的其他值,看看是否读取了新值。

  • 请注意,此解决方案存在巨大的局限性:http://android-developers.blogspot.com/2011/03/identifying-app-installations.html (2认同)