Configurar Admob con Google Play Services en Android

1. Agregar en build.gradle la linea 'com.google.android.gms...'

apply plugin: 'android'
...

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.google.android.gms:play-services:5.0.77'
}

IR A TOOLS / ANDROID / SYNC PROJECT WITH GRADLE FILES y esperar a que las referencias queden bien (no queden en rojo)

2. Agregar un import en el .java

import com.google.android.gms.ads.*;

3. Agregar en AndroidManifest.xml (dentro del tag <application>)

        <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

4. Antes del tag <application> agregar:

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

5. Usar este codigo en el MyActivity.java

package com.expressraider.admob;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.google.android.gms.ads.*;

public class MyActivity extends Activity {

    private AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);

        //------------- ADS --------------------------//
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        // Create a banner ad. The ad size and ad unit ID must be set before calling loadAd.
        mAdView = new AdView(this);
        mAdView.setAdSize(AdSize.SMART_BANNER);
        mAdView.setAdUnitId("ca-app-pub-0665940275249022/9155234413");

        mAdView.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));

        TelephonyManager teleManager =(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
        String device_id=teleManager.getDeviceId();

        //obtengo el id del telefono, para agregarlo como dispositivo de testing
        String android_id = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
        String deviceId = md5(android_id).toUpperCase();

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

        FrameLayout.LayoutParams initialFrm = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        this.addContentView(mAdView, initialFrm);

        mAdView.loadAd(adRequest);
    }

    @Override
    public void onResume() {
        super.onResume();

        // Resume the AdView.
        mAdView.resume();
    }

    @Override
    public void onPause() {
        // Pause the AdView.
        mAdView.pause();

        super.onPause();
    }

    @Override
    public void onDestroy() {
        // Destroy the AdView.
        mAdView.destroy();

        super.onDestroy();
    }
}



6. Si da error cuando se ejecuta, ver en el LogCat esta linea:


7. En mAdView.setAdUnitId hay que poner el pub ID, que se obtiene de admob de esta forma:

ir a admob
ir a ficha Monetizar
ver en los bloques de anuncios, que dice ca-app-pub-0665940275249022/9155234413
ese es el que hay que poner.





ca-app-pub-0665940275249022/9155234413

No hay comentarios:

Publicar un comentario