Created
February 22, 2017 12:20
-
-
Save hector6872/4786d2b726bf40be283681b6af6a1dff to your computer and use it in GitHub Desktop.
PlayServicesUtils - Get Advertising Id
This file contains hidden or 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
| public class PlayServicesUtils { | |
| private static String advertisingId = ""; | |
| private static boolean limitAdTrackingEnabled = false; | |
| private PlayServicesUtils() { | |
| } | |
| public static void prepareAdvertisingId(final Context context) { | |
| if (TextUtils.isEmpty(advertisingId)) { | |
| new Thread(new Runnable() { | |
| public void run() { | |
| advertisingId = internalGetAdvertisingId(context); | |
| limitAdTrackingEnabled = internalIsPlayTrackingEnabled(context); | |
| } | |
| }).start(); | |
| } | |
| } | |
| private static String internalGetAdvertisingId(Context context) { | |
| try { | |
| Object AdvertisingInfoObject = getAdvertisingIdInfoObject(context); | |
| return (String) invokeInstanceMethod(AdvertisingInfoObject, "getId", null); | |
| } catch (Throwable t) { | |
| return null; | |
| } | |
| } | |
| private static Boolean internalIsPlayTrackingEnabled(Context context) { | |
| try { | |
| Object AdvertisingInfoObject = getAdvertisingIdInfoObject(context); | |
| Boolean isLimitedTrackingEnabled = | |
| (Boolean) invokeInstanceMethod(AdvertisingInfoObject, "isLimitAdTrackingEnabled", null); | |
| return isLimitedTrackingEnabled == null ? false : isLimitedTrackingEnabled; | |
| } catch (Throwable t) { | |
| return false; | |
| } | |
| } | |
| private static Object getAdvertisingIdInfoObject(Context context) throws Exception { | |
| return invokeStaticMethod("com.google.android.gms.ads.identifier.AdvertisingIdClient", "getAdvertisingIdInfo", | |
| new Class[] { Context.class }, context); | |
| } | |
| private static Object invokeStaticMethod(String className, String methodName, Class[] classArgs, Object... args) | |
| throws Exception { | |
| Class classObject = Class.forName(className); | |
| return invokeMethod(classObject, methodName, null, classArgs, args); | |
| } | |
| private static Object invokeMethod(Class classObject, String methodName, Object instance, Class[] classArgs, | |
| Object... args) throws Exception { | |
| @SuppressWarnings("unchecked") Method methodObject = classObject.getMethod(methodName, classArgs); | |
| if (methodObject == null) { | |
| return null; | |
| } | |
| return methodObject.invoke(instance, args); | |
| } | |
| private static Object invokeInstanceMethod(Object instance, String methodName, Class[] classArgs, Object... args) | |
| throws Exception { | |
| Class classObject = instance.getClass(); | |
| return invokeMethod(classObject, methodName, instance, classArgs, args); | |
| } | |
| public static String getAdvertisingId() { | |
| return advertisingId; | |
| } | |
| public static boolean isLimitAdTrackingEnabled() { | |
| return limitAdTrackingEnabled; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment