1

I am trying to create an app compatible to Android SDK 19 (Kitkat) that can generate a unique id. I am currently using com.google.android.gms:play-services:7.0.0 (because I see it is compatible to Kitkat) , and my code looks something like this

final int answer = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
if (answer != ConnectionResult.SUCCESS) {
    Log.w(TAG, "Google Play Services is NOT available. Status Code is " + answer);
    return null;
}

// running in async
try {
    final AdvertisingIdClient.Info advertisingIdInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
    return advertisingIdInfo.getId();
} catch (Exception e) {
    Log.w(TAG, "Unable to generate Advertising id info due to " + e.getMessage());
    e.printStackTrace();
}

When running this against my emulator (Nexus 5 + Android 19 + Intel x86), I am getting ConnectionResult.SUCCESS in my if statement, but still getting a GooglePlayServicesNotAvailableException.

I would like to know why, but can't seem to figure out.

Any ideas why?

W/my.Class( 2376): Unable to generate Advertising id info due to null

W/System.err( 2376): com.google.android.gms.common.GooglePlayServicesNotAvailableException

W/System.err( 2376): at com.google.android.gms.ads.identifier.AdvertisingIdClient.j(Unknown Source)

W/System.err( 2376): at com.google.android.gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(Unknown Source)

W/System.err( 2376): at my.Class$1$1.doInBackground(Z.java:115)

W/System.err( 2376): at android.os.AsyncTask$2.call(AsyncTask.java:288) W/System.err( 2376): at java.util.concurrent.FutureTask.run(FutureTask.java:237)

W/System.err( 2376): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)

W/System.err( 2376): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

W/System.err( 2376): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

W/System.err( 2376): at java.lang.Thread.run(Thread.java:841)

Thanks, Franz

Franz See
  • 3,282
  • 5
  • 41
  • 48

1 Answers1

0

That might be because most of the methods in GooglePlayServicesUtil class is deprecated including [isGooglePlayServicesAvailable][1]. You can use below given code to check availability of google play services.

private boolean checkPlayServices() {
        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (apiAvailability.isUserResolvableError(resultCode)) {
                apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
                        .show();
            } else {
                Log.i(TAG, "This device is not supported.");
                finish();
            }
            return false;
        }
        return true;
}

Update :- You need to upgrade play-service version from 7.0.0 to 8.4.0 in order to use above given code. Also check this answer.

Community
  • 1
  • 1
Pravin Divraniya
  • 4,223
  • 2
  • 32
  • 49