I am using advertising id in my app and the code was working fine util I tested it on a device running Android Nougat 7.1.2. Can you point out what should I add to get it in Nougat too ?
private static void getAdvertisingID() {
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
AdvertisingIdClient.Info idInfo = null;
try {
idInfo = AdvertisingIdClient.getAdvertisingIdInfo(_context);
if (idInfo.isLimitAdTrackingEnabled()){ // check if user has opted out of tracking
Log.i("NetworkStateChangeReceiver","Not Found");
return "did not found GAID... sorry";
}
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
String advertId = null;
try{
advertId = idInfo.getId();
}catch (Exception e){
e.printStackTrace();
}
return advertId;
}
@Override
protected void onPostExecute(String advertId) {
Log.i("NetworkStateChangeReceiver","Found "+advertId);
UtilHelper.setDeviceIDFA(advertId);
}
};
task.execute();
}
Turns out the code works fine, only problem is that on Nougat it takes some to call onPostExecute
method, while on other devices running lower android version this method is called quickly. I just added some delay to get the advertisement id after calling getAdvertisingID
and everything worked out fine.
Happy coding