Liam
Liam

Reputation: 53

Admob ads displaying on emulator but not real device

I have a simple activity just to test the display of ads:

public class AdTestActivity extends Activity {

private AdView adView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
    setContentView(R.layout.ad_test);

    adView = new AdView(this, AdSize.BANNER, "[my ad id]");
    LinearLayout layout = (LinearLayout)findViewById(R.id.layout_at_1);
    layout.addView(adView);
    adView.loadAd(new AdRequest());
}

@Override
  public void onDestroy() {
    if (adView != null) {
      adView.destroy();
    }
    super.onDestroy();
  }
}

Ads are working fine on emulators, and on my phone over ADB. However, if I create a .apk of the project and install it on the same phone (after uninstalling the one from ADB), it force closes as soon as I open the AdTestActivity. The activity is declared in the manifest:

    <activity
        android:name=".AdTestActivity"
        android:label="@string/title_activity_main"
        android:screenOrientation="portrait" >
    </activity>

I've declared the permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Any idea what's going on?

Edit- yes, I've disabled ad blocking on the phone.

Upvotes: 2

Views: 1165

Answers (2)

Name is Nilay
Name is Nilay

Reputation: 2783

Generate the id by this following code:

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


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) {
        e.printStackTrace();
    }
    return "";
}

and use this in:

AdRequest adRequest = new AdRequest.Builder().addTestDevice(deviceId).build();

Also if this fails, google give you that device id in the logcat. Please see that too !!

Upvotes: 0

Sulabh Gupta
Sulabh Gupta

Reputation: 642

Are you getting ClassNotFoundException or ActivityNotFoundException after installing APK?

If so, Please export AdMod jar file in your Build Path-> Order and Export

Upvotes: 1

Related Questions