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

4

6

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;
    }
}
Stepfather answered 30/3, 2014 at 10:53 Comment(2)
What is the URI here in the code snippet?Hadfield
sorry I forgot to put it in the snippet, now it's okStepfather
S
5

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.

Stepfather answered 17/4, 2014 at 13:14 Comment(3)
Did you have to add com.google.android.providers.gsf.permission.READ_GSERVICES permission?Midrash
Well, I had to in my case (Nexus 5 with Android 5.0), otherwise an exception is raised. Can you verify if it's working for your users with a Nexus 5 with Android 5.0?Midrash
@Midrash you have to add the pemission com.google.android.providers.gsf.permission.READ_GSERVICESRipen
K
2

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;
    }
}
Known answered 26/4, 2017 at 4:3 Comment(0)
M
1

Can't talk about production tests, but I noticed that in my Nexus 5 with Android 5.0, I had to add the following permission: com.google.android.providers.gsf.permission.READ_GSERVICES. Otherwise an exception is raised when using your code.

Midrash answered 2/1, 2015 at 20:50 Comment(0)
G
0

Can't talk about production tests, but I noticed that in my Nexus 5 with Android 5.0, I had to add the following permission: com.google.android.providers.gsf.permission.READ_GSERVICES. Otherwise an exception is raised when using your code.

Share Improve this answ

Giralda answered 24/9, 2021 at 16:14 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Broucek

© 2022 - 2024 — McMap. All rights reserved.