Last active
March 8, 2019 03:20
-
-
Save Ray33/942821e8297443fcb785eb04a3f13119 to your computer and use it in GitHub Desktop.
An Asynchronous method to get user google advertising id.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//callback for user id: | |
//Either takes the user's advertiserId or creates a unique ID, if | |
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() { | |
@Override | |
protected String doInBackground(Void... params) { | |
try { | |
AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(YourApplicationClass.this); | |
if (!adInfo.isLimitAdTrackingEnabled()) {//if user hasn't opt-out | |
return adInfo.getId(); | |
}else{ | |
//Settings.Secure.ANDROID_ID: A 64-bit number (as a hex string) that is randomly | |
// generated when the user first sets up the device and should remain | |
// constant for the lifetime of the user's device. | |
//String android_id = Settings.Secure.getString(YourApplicationClass.this.getContentResolver(), | |
// Settings.Secure.ANDROID_ID); | |
//if ANDROID_ID is unavailable - generate a random ID | |
//return android_id == null ? new UUID(System.currentTimeMillis(), System.currentTimeMillis()*2).toString() : android_id; | |
return "";// Respect user privacy and return nothing | |
} | |
} catch (Exception e) { | |
Log.e(TAG, e.getMessage(), e); | |
} | |
//default return is empty, in order to catch issues with user id generation | |
return ""; | |
} | |
@Override | |
protected void onPostExecute(String advertId) { | |
userID = advertId; // save it for future use | |
} | |
}; | |
task.execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment