Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GSF ID KEY (google service framework id) as Android device unique identifier

I need to uniquely identify an Android device. I read about ANDROID_ID but it seems to have problems with Android 2.2. Then there are other identifiers related to TelephonyManager, but I reckon they don't work on tablets.
So, looking for something working on every device I stumbled upon the GSF ID KEY (google service framework id). Do you guys think this is a reliable and always working solution? This is the code I found to retrieve the GSF ID KEY:

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;
    }
}
like image 628
user2461515 Avatar asked Mar 30 '14 10:03

user2461515


People also ask

What is a GSF ID?

The Google Services Framework Identifier (GSF ID) is a unique 16 character hexadecimal number that your device automatically requests from Google as soon as you log to your Google Account for the first time. For a specific device, the GSF ID will change only after a factory reset.

What is unique device id in Android?

The definition of device ID A device ID is a unique, anonymized string of numbers and letters that identifies every individual smartphone or tablet in the world. It is stored on the mobile device and can be retrieved by any app that is downloaded and installed.


2 Answers

In case someone is wondering if this method works the answer is yes, I tried it (and used it in an app I put on the Android market with thousands of downloads) and it works. Note: the GSF ID KEY changes every time the user does a factory reset or messes up with Google Services, but it was good enough for my purpose.

like image 191
user2461515 Avatar answered Oct 13 '22 00:10

user2461515


using Android Studio, I get auto-recommendations from lint. here is the code, after revised. it may solve exceptions reported by https://stackoverflow.com/users/423171/cprcrack

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;
    }
}
like image 2
Dika Avatar answered Oct 12 '22 23:10

Dika