如何获取Admob需要的的test device ID

官方指南说


请求一个广告就会打印id出来


he alternative way of serving test ads is to keep your regular ad unit ID, but configure your device as a test device. When a device is configured as a test device, the Google Mobile Ads SDK automatically substitutes in the aforementioned sample ad unit ID, limiting that device to receiving test ads.


Follow the steps below to configure your device as a test device.


Note: Android emulators are automatically configured as test devices.
Add your test device


Load your ads-integrated app and make an ad request.
Check the logcat output for a message that looks like this:
I/Ads: Use AdRequest.Builder.addTestDevice("33BE2250B43518CCDA7DE426D04EE232")
to get test ads on this device.
Copy your test device ID to your clipboard.
Modify your code to call AdRequest.Builder.addTestDevice() with your test device ID. This method can be called multiple times for multiple devices.
AdRequest request = new AdRequest.Builder()
    .addTestDevice("33BE2250B43518CCDA7DE426D04EE232")
    .build();
You can optionally check request.isTestDevice() to confirm that your device was properly added as a test device.


Note: Be sure to remove the code that sets these test devices before you release your app.




但是我的手机没输出

酷派的手机好像 屏蔽了i级别的打印.


下面是网上查到的方法, 用代码获取id.


源地址


The accepted answers will work if you are only testing on the Emulator or on a few devices, but if you are testing on a plethora of devices, you may need some means of prorammatically adding the running device's device ID.


The following code will make the current running device into an adview test device programmatically

if(YourApplication.debugEnabled(this)) //debug flag from somewhere that you set
    {

        String android_id = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
        String deviceId = md5(android_id).toUpperCase();
        mAdRequest.addTestDevice(deviceId);
        boolean isTestDevice = mAdRequest.isTestDevice(this);

        Log.v(TAG, "is Admob Test Device ? "+deviceId+" "+isTestDevice); //to confirm it worked
    }
You need to use the md5 of the Android ID, and it needs to be upper case. Here is the md5 code I used

public 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) {
        Logger.logStackTrace(TAG,e);
    }
    return "";
}

我用其他人的一加手机测试, 获得的ID和logcat打印的是一样的.

你可能感兴趣的:(android)