Created
November 12, 2013 16:16
-
-
Save nazgee/7433663 to your computer and use it in GitHub Desktop.
code snippet to request test ads on "unknown" device
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 static void addThisDeviceAsTestDevice(AdRequest pAdRequest) { | |
String android_id = Settings.Secure.getString(GameResources.ACTIVITY.getContentResolver(), Settings.Secure.ANDROID_ID); | |
String deviceId = md5(android_id).toUpperCase(); | |
pAdRequest.addTestDevice(deviceId); | |
boolean isTestDevice = pAdRequest.isTestDevice(GameResources.ACTIVITY); | |
if (!isTestDevice) { | |
throw new RuntimeException("Failed to use test Ads :("); | |
} | |
} | |
private static final String md5(final String s) { | |
try { | |
// Create MD5 Hash | |
MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); | |
digest.update(s.getBytes()); | |
byte messageDigest[] = digest.digest(); | |
// Create Hex String | |
StringBuffer hexString = new StringBuffer(); | |
for (int i = 0; i < messageDigest.length; i++) { | |
String h = Integer.toHexString(0xFF & messageDigest[i]); | |
while (h.length() < 2) | |
h = "0" + h; | |
hexString.append(h); | |
} | |
return hexString.toString(); | |
} catch (NoSuchAlgorithmException e) { | |
throw new RuntimeException(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment