Search code examples
javaandroidandroid-studiointerstitial

Getting device ID to show Admob Interstitial Ads


I have previously used Admob, but only to display Banner Ads. Now on my latest App I'd like to use Interstitial Ads, but I have some doubts about it.

According to the documentation, in order to request for a new Ad, I need to do something like this:

 AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice("Device_ID")
                .build();

The problem is that I don't know how to get the device ID programatically, since I guess it's a different one on each device. What I've done so far to dislpay Interstitial test ads on my device is calling the .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) method. So it's pretty much working fine on my device.

The main problem is that I want to be able to display interstitial ads on multiple devices, but I sincerely don't know how to get the ID programatically to get it to work in any device.

Thanks in advance!


Solution

  • Here is what I usually use on my apps :

    public static String getMD5(String inputText){
         String md5 = "";
         try{
            MessageDigest digester = MessageDigest.getInstance("MD5");
            digester.update(inputText.getBytes());
            md5 = new BigInteger(1, digester.digest()).toString(16);
         }
         catch(Exception e){}
         return md5;
    }
    
    
    
    public String getDeviceId(){
         String androidID = Settings.Secure.getString(getContentResolver(),Settings.Secure.ANDROID_ID);
         String deviceID = getMD5(androidID).toUpperCase();
         return deviceID;
    }