How to programmatically get the Device ID for Admo

2019-03-29 08:32发布

问题:

I have multiple devices, and I probably will have more, and do not want to add them one by one. Does anybody know what ID Admob uses?

回答1:

String aid = Settings.Secure.getString(getContext().getContentResolver(), "android_id");

Object obj = null;
try {
    ((MessageDigest) (obj = MessageDigest.getInstance("MD5"))).update(
                                   aid.getBytes(), 0, aid.length());

    obj = String.format("%032X", new Object[] { new BigInteger(1,
                                   ((MessageDigest) obj).digest()) });
} catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
    obj = aid.substring(0, 32);
}

I hope this will help you ;)



回答2:

I tried that and it gave me the Hex value for my ESN number. This is not the number AdMob uses.

When requesting an ad, in the Dalvic Debug Monitor log it shows you the number to use.

Also you might want to refer to the following page (about half way down): http://code.google.com/mobile/ads/docs/android/intermediate.html



回答3:

Hi Note Down My answer ,if you want to get Device id, you can use TelephonyManager as i used below

String device_id=null;
TelephonyManager telemngr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
device_id=telemngr.getDeviceId();

You also need to add the following permission to your manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />