Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique Android Device ID after MarshMallow warning "get device identifiers is not recommended"?

Tags:

I saw one of my best question Is there a unique Android device ID?

Edited:

I need a solution for Android 10 too.

I used some following code to get Unique Id.

public static String getDeviceId(Activity context) {

    PermissionsChecker checker = new PermissionsChecker(context);

    if (checker.lacksPermissions(Manifest.permission.READ_PHONE_STATE))
        PermissionsActivity.startActivityForResult(context, 0, REQUIRED_PERMISSION);
    else {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        final String tmDevice = tm.getDeviceId();
        final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

        UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32));

        return deviceUuid.toString();
    }
    return null;
}

But I am getting some warning on hover on

tm.getDeviceId();

and

Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); as following:

Using getString to get device identifiers is not recommended.

Using device identifiers is not recommended other than for high value fraud prevention and advanced telephony use-cases. For advertising use-cases, use AdvertisingIdClient$Info#getId and for analytics, use InstanceId#getId.

Is there any solution? Is it harmful or anything else?

like image 289
Pratik Butani Avatar asked Oct 27 '16 11:10

Pratik Butani


2 Answers

The very easy answer is if you need to uniquily identify each user of your app create a random id at first use.

    UUID.randomUUID().toString()

You can save it in SharedPreferences and the easy work is done. No permissions needed

The problem is that it wont help you if user unninstall and reinstall the app.

One easy trick that is possible to do until android 10 (and wont be possible anymore on android 11 according to google) is save it in a hidden file in the external storage...

then on first use of app you look for such file, if it exists read the UUID, case not generate a new one and save the file.

The problem with this strategy is YOU NEED WRITE_EXTERNAL_STORAGE PERMISSION

in case this isnt a problem simple do...

new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + ".my_hidden_folder").mkdir();

Any file saved inside this folder will be hidden to user, wont be removed on unninstall of your app

I personally use

Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

This isn't harmful is just that google dont like you doing it

like image 55
Rafael Lima Avatar answered Sep 24 '22 18:09

Rafael Lima


It's the limitation added in Andorid Q that getDeviceId will work for only system Apps.If you try to use this it will throw exception

SecurityException: getDeviceId: The user 10196 does not meet the requirements to access device identifiers.

You can read more Andorid Q Changes from this link

Anyhow after searching and seeing many solutions i found this one which is working for every Android for getting UUID works great.

//This method is fully compatible with Android 1 to Android 10
public static String getDeviceUUID(Context context) {
    String serial = null;
    String m_szDevIDShort = "35" +
            Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +
            Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +
            Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +
            Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +
            Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +
            Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +
            Build.USER.length() % 10; //13 Bit
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            serial = android.os.Build.getSerial();
        } else {
            serial = Build.SERIAL;
        }
        //API>=9 Use serial number
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    } catch (Exception exception) {
        //serial Need an initialization
        serial = "serial"; // Random initialization
    }
    // 15-digit number cobbled together using hardware information
    return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}
like image 34
Fahid Mahmood Avatar answered Sep 23 '22 18:09

Fahid Mahmood