Advertisingidclient Getadvertisingidinfo Blocked By Main Thread
Solution 1:
To get the google ad ID you need not to run the method getAdvertisingIdInfo
on the main thread.
I use Async Task to manage the extraction of the google ad ID.
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import java.io.IOException;
publicclassMainActivityextendsAppCompatActivity {
StringGAID; // this is the String of the Google Ad ID that you'll receive upon onPostExecute@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newGetGAIDTask().execute();
}
privateclassGetGAIDTaskextendsAsyncTask<String, Integer, String> {
@OverrideprotectedStringdoInBackground(String... strings) {
AdvertisingIdClient.Info adInfo;
adInfo = null;
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(MainActivity.this.getApplicationContext());
if (adInfo.isLimitAdTrackingEnabled()) // check if user has opted out of trackingreturn"did not found GAID... sorry";
} catch (IOException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
}
return adInfo.getId();
}
@OverrideprotectedvoidonPostExecute(String s) {
GAID = s;
}
}
You also need to add to the app build.gradle on the dependencies the line
compile'com.google.android.gms:play-services-ads:7.8.0'
And be sure you have on the Android SDK manager the "EXTRAS Google Repository" updated
Solution 2:
The issue you are seeing, where no amount of time seems long enough for the getAdvertisingIdInfo call to complete, is caused by how you are waiting and how runOnUiThread works. The key is that runOnUiThread will queue the code to be run after what is currently running on the ui thread, in this case the onCreate. The sleep calls used to "simulate waiting" will let your background thread run and do its work, but the final operation to set m_info will always be queued and executed after onCreate completes.
One solution would be to ensure that m_info is safe to access from multiple threads and simply assign it on the background thread. There would be no need for runOnUiThread. This would remove queuing, and allow your code to work with minimal changes.
A better solution would be to keep the use of runOnUiThread and remove the sleep used to wait. You would need to keep in mind that m_info will always be null in onCreate, but other events can check if the value is non null and use it as needed.
https://developer.android.com/reference/android/app/Activity#runOnUiThread(java.lang.Runnable)
Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.
Solution 3:
In the literature about AdvertisingIdClient it says to not use on main thread. It will throw an exception. So if you put it into it's own thread you will be fine, most likely.
Solution 4:
According to the docs (here) :
Unfortunately, in these cases using the com.google.android.gms.iid InstanceID API or system functions to create an app-scoped ID are not appropriate solutions because the ID may need to be shared across apps. An alternative solution is to use the Advertising Identifier available from the AdvertisingIdClient.Info class via the getId() method. You can create an AdvertisingIdClient.Info object using the getAdvertisingIdInfo(Context) method and call the getId() method to use the identifier. Note that this method is blocking, so you should not call it from the main thread; a detailed explanation of this method is available here.
Also here:
public static AdvertisingIdClient.Info getAdvertisingIdInfo (Context context)
Retrieves the user's advertising ID and limit ad tracking preference.
This method cannot be called in the main thread as it may block leading to ANRs. An IllegalStateException will be thrown if this is called on the main thread.
So they say it's blocking... You need to put this code in a background thread.
Post a Comment for "Advertisingidclient Getadvertisingidinfo Blocked By Main Thread"