本文介绍了Android的唯一的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何得到一个唯一的ID从Android手机?

How do I get an unique ID from an Android phone?

每当我尝试从手机的唯一ID作为字符串它始终显示的 Android的ID 的,没有其他独特的十六进制值。

Whenever I try to get the unique ID from the phone as a string italways shows android id and no other unique hex values.

我如何得到一个?

这是在code我用得到的ID至今:

This is the code I use to get the ID until now:

String id=Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);
Log.i("Android is is:",id);

这是我得到的输出是这样的:

the output which I get looks like this:

Android id is: android id

我使用的是Nexus One的测试。

I am using a Nexus One for testing.

推荐答案

有关如何获得一个唯一的标识符为您的应用程序从安装在每台Android设备,请参阅本官方Android开发者博客上详细说明:

For detailed instructions on how to get a Unique Identifier for each Android device your application is installed from, see this official Android Developers Blog posting:

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

这似乎是最好的办法是让你在安装时产生一个你的自我,并随后读它时,应用程序重新启动。

It seems the best way is for you to generate one your self upon installation and subsequently read it when the application is re-launched.

我个人觉得这可以接受,但不理想。没有搭载Android提供了一个标识符中的所有实例的工作原理大部分都依赖于手机的无线电状态(打开/ WiFi关闭,手机开/关,蓝牙开/关)。在其他类似Settings.Secure.ANDROID_ID必须由制造商来实施,并且不保证是唯一的。

I personally find this acceptable but not ideal. No one identifier provided by Android works in all instances as most are dependent on the phone's radio states (wifi on/off, cellular on/off, bluetooth on/off). The others like Settings.Secure.ANDROID_ID must be implemented by the manufacturer and are not guaranteed to be unique.

以下是将数据写入到一个文件安装,将连同应用程序保存在本地的任何其他数据存储的一个例子。

The following is an example of writing data to an INSTALLATION file that would be stored along with any other data the application saves locally.

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();
    }
}

这篇关于Android的唯一的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:56