Failed to get android ad id

I use the code below to get the ad id in android, but this gives the following exception.

06-12 12:14:19.034: E/AndroidRuntime(13631): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.authorwjf.amianemulator/com.authorwjf.amianemulator.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.ads.identifier.AdvertisingIdClient$Info.getId()' on a null object reference 

I added the google play services library and meta tag in the Android manifest file.

I use the below code in the oncreate activity method.

  Info adInfo = null; try { adInfo = AdvertisingIdClient.getAdvertisingIdInfo(this); } catch (IOException e) { } catch (GooglePlayServicesNotAvailableException e) { } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (GooglePlayServicesRepairableException e) { // TODO Auto-generated catch block e.printStackTrace(); } String AdId = adInfo.getId(); System.out.println("AdId :: "+AdId); 

Some doubts ::

  • Does genymotion support an advertising identifier?

  • Is the advertising identifier available on all Android phones that use 4.0 and above?

Please help me get the ad id.

+5
source share
1 answer

You get this exception because Google Play services are not installed on the device (or emulator).

As you can read in the Google Play Services documentation:

Note. Since it’s difficult to predict the status of each device, you should always check for compatible Google Play APK service access to Google Play features.

Link: https://developers.google.com/android/guides/setup#ensure_devices_have_the_google_play_services_apk

You can check if Google Play services are installed using isGooglePlayServicesAvailable , for example:

 if(GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) { //Google Play Services are available } else { //Google Play Services are not available, or not updated } 

Does genymotion support an advertising identifier?

Yes, but you need to install the Google Play service manually. See this answer .

Is the ad identifier available on all Android phones that use 4.0 and above?

No, Google Play services may not be available.

+7
source

All Articles