简体   繁体   中英

How to get Device id for Admob

i added admob ad in my application, the device id required for admob i find by following code,

String android_id = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
device_id = md5(android_id).toUpperCase();

public static String md5(String s) {
    try {

        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();


        StringBuffer hexString = new StringBuffer();
        for (int i=0; i<messageDigest.length; i++)
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

The problem with this is that its showing ads on some devices but on some devices it shows test ads. where am i wrong?? pls help

addTestDevice is used for showing Admob test ads for development.so in deployment remove addTestDevice from your adRequest ,ie make your adRequest as

   AdRequest adRequest = new AdRequest.Builder().build();
   adView.loadAd(adRequest);

And if you want to get device id for testing then see this How can I get device ID for Admob Then add addTestDevice in adRequest as

AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(device_id)
                .build();
adView.loadAd(adRequest);

It is mentioned on the official tutorial page with link - https://developers.google.com/mobile-ads-sdk/docs/admob/android/banner That

logcat will print the device's MD5-hashed ID for convenience, for example: Use AdRequest.Builder.addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") to get test ads on this device.

Means your device ID will be similarly shown. The above one is just an example id.

Just go to your Log Cat and search "Ads". Then you will find your device then just use it like.

AdRequest request = new AdRequest.Builder()
.addTestDevice("Device ID")
.build();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM