创建一个全局唯一标识符的Andr​​oid [英] Creating a Globally Unique Android Identifier

查看:190
本文介绍了创建一个全局唯一标识符的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当谈到独特的Andr​​oid的ID,我相信大家都看过,但我也正在尝试拿出一个解决方案来唯一标识任何Android设备。我会很乐意使用公开发布的类,但我还没有找到一个。

When talking about unique Android ID's, I'm sure everyone has seen this, however I too am trying to come up with a solution to uniquely identify any android device. I would be happy to use a publicly released class but I am yet to find one.

在我的情况下,要求是能够唯一标识具有某种形式的互联网连接(如GPRS或Wi-Fi)的任何设备,并且运行在API级别8(V2.2升级Froyo)。我不知道有任何的设备不具有Wi-Fi或SIM卡,所以让我知道如果有任何!

In my situation, the requirement is to be able to uniquely identify any devices that have some form of an internet connection (such as GPRS or Wi-Fi) and run on API level 8 (v2.2 Froyo). I'm not aware of any devices that doesn't have Wi-Fi or a SIM so let me know if there is any!

我们使用了Wi-Fi MAC地址的哈希值,因为所有的iOS设备的解决了iOS的这个问题应该的都没有了。然而,对于Android的技术(如答案的SO后上文)指的是 TelephonyManager 类,它可以返回根据该或的getContext()。getContentResolver(),Secure.ANDROID_ID (例如,当设备不具有的SIM连接,像与Nexus 7) 这里之前升级Froyo是不是版本的100%可靠的(这幸运的是我)。但是,它没有任何问题后Froyo的状态,以便再次,如果有任何,让我知道!我也看到了这个可以返回。据<一href="http://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID">here它可以更改恢复出厂设置,但这不是一个大问题。

We solved this problem in iOS by using a hash of the Wi-Fi mac address as all iOS devices should have this. However for Android, techniques (such as the answers to the SO post referenced above) refer to the TelephonyManager class which can return null (such as when the device doesn't have a SIM connection, like with the Nexus 7) or getContext().getContentResolver(), Secure.ANDROID_ID which according to here isn't 100% reliable on versions prior to Froyo (which is fortunate for me). It doesn't however state of any issues post-Froyo so again, if there are any, let me know! I've also read this can return null. According to here it can change on a factory reset, but that isn't a major concern.

所以我把这个在一起,产生一个希望唯一-GUID: [这种方法是不完整的!]

So I put together this to generate a hopefully-unique-GUID: [This method is not complete!]

public static String GetDeviceId(Context context)
{
    // Custom String Hash 1
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("someRandomData"); // Not really needed, but means the stringBuilders value won't ever be null

    // TM Device String
    final TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    String tmDeviceId = tm.getDeviceId(); // Could well be set to null!
    LogMsg.Tmp("TM Device String [" + tmDeviceId + "]");

    // Custom String Hash 2
    stringBuilder.append(tmDeviceId);
    int customHash = stringBuilder.toString().hashCode();
    LogMsg.Tmp("Custom String hash [" + customHash + "]");

    // Device ID String
    String androidIDString = android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
    LogMsg.Tmp("Device ID String [" + androidIDString + "]");

    // Combined hashes as GUID
    UUID deviceUuid = new UUID(androidIDString.hashCode(), ((long)customHash << 32));
    LogMsg.Tmp("Combined hashes as GUID [" + deviceUuid.toString() + "]");

    return deviceUuid.toString();
}

所以,你可能会发现 tmDeviceId 被设置为,在这种情况下的 customHash 将不管设备是相同的,但那么这种结合 androidIDString 应该是全局唯一的。我觉得。很显然,我需要工作,如果 tmDeviceId androidIDString 不可用,并抛出一个异常那里。

So you may find tmDeviceId being set to null, in this case the the customHash will be the same regardless of the device but then this combined with androidIDString should be globally unique. I think. Obviously I will need to work out if tmDeviceId AND androidIDString aren't available and throw an exception there.

所以...这是矫枉过正?如果是这样,它是安全的,只要使用 context.getContentResolver(),android.provider.Settings.Secure.ANDROID_ID

So... is this overkill? If so, is it safe to just use context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID?

推荐答案

你为什么不拿到设备的MAC地址作为你的iOS已经做了什么?这可以在Android设备以及进行。

Why don't you get MAC address of the device as you've done in iOS? This can be performed in Android devices as well.

我会给其获得设备的MAC地址的code段。

I'll give a code snippet that obtains mac address of the device..

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class HardwareUtil {

    public static String getMacAddress()
    {
        try{
            Process process = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0){
                output.append(buffer, 0, read);
            }
            reader.close();
            process.waitFor();
            String hwaddr = output.toString();
            return hwaddr;
        }catch (IOException e) {
            e.printstacktrace();
        }catch (InterruptedException e){
            e.printstacktrace();
        }
    }

}

HardwareUtil.getMacAddress()将返回设备的MAC地址。

HardwareUtil.getMacAddress() will return mac address of the device.

编辑:如果MAC地址不适合您的情况。下面可能是有用的!

public static String getDeviceId(Context context) {
    final String deviceId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    if (deviceId != null) {
        return deviceId;
    } else {
        return android.os.Build.SERIAL;
    }
}

这篇关于创建一个全局唯一标识符的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆