Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GSF ID密钥(谷歌服务框架ID)作为Android设备唯一标识符_Android_Android Identifiers - Fatal编程技术网

GSF ID密钥(谷歌服务框架ID)作为Android设备唯一标识符

GSF ID密钥(谷歌服务框架ID)作为Android设备唯一标识符,android,android-identifiers,Android,Android Identifiers,我需要唯一地识别安卓设备。我读过关于ANDROID_ID的文章,但它似乎在ANDROID 2.2上有问题。还有其他与TelephonyManager相关的标识符,但我认为它们在平板电脑上不起作用。 因此,我偶然发现了GSF ID密钥(google服务框架ID),希望在每台设备上都能找到工作。你们认为这是一个可靠且始终有效的解决方案吗? 这是我找到的用于检索GSF ID密钥的代码: private static String getGsfAndroidId(Context context) {

我需要唯一地识别安卓设备。我读过关于ANDROID_ID的文章,但它似乎在ANDROID 2.2上有问题。还有其他与TelephonyManager相关的标识符,但我认为它们在平板电脑上不起作用。
因此,我偶然发现了GSF ID密钥(google服务框架ID),希望在每台设备上都能找到工作。你们认为这是一个可靠且始终有效的解决方案吗? 这是我找到的用于检索GSF ID密钥的代码:

private static String getGsfAndroidId(Context context) 
{
    Uri URI = Uri.parse("content://com.google.android.gsf.gservices");
    String ID_KEY = "android_id";
    String params[] = {ID_KEY};
    Cursor c = context.getContentResolver().query(URI, null, null, params, null);
    if (!c.moveToFirst() || c.getColumnCount() < 2)
        return null;
    try 
    {
        return Long.toHexString(Long.parseLong(c.getString(1)));
    } 
    catch (NumberFormatException e) 
    {
        return null;
    }
}
私有静态字符串getgsfandroid(上下文)
{
Uri=Uri.parse(“content://com.google.android.gsf.gservices");
String ID_KEY=“android_ID”;
字符串参数[]={ID_KEY};
游标c=context.getContentResolver().query(URI,null,null,params,null);
如果(!c.moveToFirst()| | c.getColumnCount()<2)
返回null;
尝试
{
返回Long.toHexString(Long.parseLong(c.getString(1));
} 
捕获(数字格式)
{
返回null;
}
}

如果有人想知道这个方法是否有效,答案是肯定的,我尝试过(并在安卓市场上的一个应用程序中使用了它,下载了数千次),它确实有效。注意:每次用户重置出厂设置或弄乱谷歌服务时,GSF ID键都会更改,但这对我来说已经足够好了。

不能谈论生产测试,但我注意到在我的Nexus 5和Android 5.0中,我必须添加以下权限:
com.Google.Android.providers.GSF.permission.READ\u GSERVICES
。否则,在使用代码时会引发异常。

使用Android Studio,我会从lint获得自动推荐。这是修改后的代码。它可以解决用户报告的异常

私有静态字符串getgsfandroid(上下文)
{
Uri=Uri.parse(“content://com.google.android.gsf.gservices");
String ID_KEY=“android_ID”;
字符串参数[]={ID_KEY};
游标c=context.getContentResolver().query(URI,null,null,params,null);
如果(c!=null&(!c.moveToFirst()| | c.getColumnCount()<2)){
如果(!c.isClosed())
c、 close();
返回null;
}
试一试{
如果(c!=null){
字符串结果=Long.toHexString(Long.parseLong(c.getString(1));
如果(!c.isClosed())
c、 close();
返回结果;
}否则{
返回null;
}
}捕获(数字格式){
如果(!c.isClosed())
c、 close();
返回null;
}
}

代码段中的URI是什么?很抱歉,我忘了将其放在代码段中,现在可以了。您是否必须添加
com.google.android.providers.gsf.permission.READ\u GSERVICES
permission?我必须添加权限(Nexus 5和android 5.0),否则会引发异常。您能否验证它是否适用于使用Android 5.0的Nexus 5的用户?@cprack您必须添加pemission com.google.Android.providers.gsf.permission.READ\u GSERVICES
private static String getGsfAndroidId(Context context)
{
    Uri URI = Uri.parse("content://com.google.android.gsf.gservices");
    String ID_KEY = "android_id";
    String params[] = {ID_KEY};
    Cursor c = context.getContentResolver().query(URI, null, null, params, null);
    if (c != null && (!c.moveToFirst() || c.getColumnCount() < 2)){
        if(!c.isClosed())
            c.close();
        return null;
    }

    try {
        if (c != null) {
            String result = Long.toHexString(Long.parseLong(c.getString(1)));
            if(!c.isClosed())
                c.close();
            return result;
        }else {
            return null;
        }
    } catch (NumberFormatException e) {
        if(!c.isClosed())
            c.close();
        return null;
    }
}