home
Android Studio
Aide
Aide Implementar anúncios

Anúncios do google em seu aplicativo


Apenas com 1(um) pacote do google, é possivel implementar anúncios em seu aplicativo através do AIDE.
Mostraremos hoje o passo-a-passo.
Confira







Primeiramente vamos abrir o aplicativo AIDE e criar um projeto vazio(simples) e importar os pacotes abaixo, não se deve copiar e colar, e sim importa-los via AIDE.



dependencies {















///////////        ANDROID STUDIO         ////////////////
        //noinspection GradleCompatible
compile 'com.android.support:appcompat-v7:27.1.1'
compile 'com.google.android.gms:play-services-ads:15.0.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
    <Button android:id="@+id/Banner"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/banner"
android:paddingTop="60dp"
android:text="Banner"
android:textStyle="bold" />

    <Button
android:id="@+id/Intersticial"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_below="@+id/Banner"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:background="@drawable/intersticial"
android:paddingTop="60dp"
android:text="Intersticial"
android:textStyle="bold" />

    <Button
android:id="@+id/Premiado"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_below="@+id/Intersticial"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:background="@drawable/premiado"
android:paddingTop="60dp"
android:text="Premiado"
android:textStyle="bold" />

    <com.google.android.gms.ads.AdView
android:id="@+id/adMob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"><!--   Id PREMIADO no AdMbo      -->
</com.google.android.gms.ads.AdView>
</RelativeLayout>



package NomeDoMeuPacote;

import android.annotation.SuppressLint;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import com.google.android.gms.ads.*;
import com.google.android.gms.ads.reward.*;

public class MainActivity extends Activity {

    AdView mBanner;
    InterstitialAd mIntersticial;
    RewardedVideoAd mPremiado;
    AdRequest BannerRequest;

    String IDAPP = "ca-app-pub-5298130832947754~2472361776";
    String mBannerTESTE = "ca-app-pub-3940256099942544/6300978111";
    String mIntersticialTESTE = "ca-app-pub-3940256099942544/1033173712";
    String mPremiadoTESTE = "ca-app-pub-3940256099942544/5224354917";

    Button Banner;
    Button Intersticial;
    Button Premiado;
    int Premio;

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

        Banner = findViewById(R.id.Banner);
        Banner.setEnabled(false);

        Intersticial = findViewById(R.id.Intersticial);
        Intersticial.setEnabled(false);

        Premiado = findViewById(R.id.Premiado);
        Premiado.setEnabled(false);

        MobileAds.initialize(this, IDAPP);/*     <-- Id do app no AdMbo        */
        mBanner = findViewById(R.id.adMob);
        BannerRequest = new AdRequest.Builder().build(); /* <-- O ID do banner esta no layout   activity_main com.google.android.gms.ads.AdView  */        
  mBanner.getWindowSystemUiVisibility();
        mBanner.loadAd(BannerRequest);
// mBanner = findViewById(R.id.adMob);        

// BannerRequest = new AdRequest.Builder()       
//  .addTestDevice("33BE2250B43518CCDA7DE426D04EE231")  
// An example device ID        
// .build();

        mIntersticial = new InterstitialAd(this);
        mIntersticial.setAdUnitId(mIntersticialTESTE);  /* <--     Id INTERSTICIAL no AdMbo        */        
        mIntersticial.loadAd(new AdRequest.Builder().build());

        mPremiado = MobileAds.getRewardedVideoAdInstance(this);
        mPremiado.loadAd(mPremiadoTESTE, new AdRequest.Builder().build());  /* <--     Id PREMIADO no AdMbo        */        
        Premio=1;

        Banner.setOnClickListener(new View.OnClickListener() {
            @Override            
        public void onClick(View v) {
                mBanner.loadAd(BannerRequest);

            }
        });
        Intersticial.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {

                if(mIntersticial.isLoaded()){
                    mIntersticial.show();
                    mIntersticial.loadAd(new AdRequest.Builder().build());
                    Toast.makeText(MainActivity.this, "Intersticial Carregado", Toast.LENGTH_SHORT).show();
                }else{
                    mIntersticial.loadAd(new AdRequest.Builder().build());
                }
            }
        });
        Premiado.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {

                if(mPremiado.isLoaded()){
                    mPremiado.show();
                    mPremiado.loadAd(mPremiadoTESTE, new AdRequest.Builder().build());
                    Toast.makeText(MainActivity.this, "show()", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "MobileAds.initialize", Toast.LENGTH_SHORT).show();
                    mPremiado.loadAd(mPremiadoTESTE, new AdRequest.Builder().build());
                }
            }
        });


        mBanner.setAdListener(new AdListener() {
            @Override    
         public void onAdLoaded() {
                Banner.setEnabled(true);
                Toast.makeText(MainActivity.this, "Banner Carregado", Toast.LENGTH_SHORT).show();
            }
        });

        mIntersticial.setAdListener(new AdListener(){
            @Override  
           public void onAdLoaded() {
                if(mIntersticial.isLoaded()){
                    Intersticial.setEnabled(true);
                    Toast.makeText(MainActivity.this, "Intersticial Carregado", Toast.LENGTH_SHORT).show();
                }else{
                    mIntersticial.loadAd(new AdRequest.Builder().build());
                }

            }
        });


        // O VIDEO PREMIADO REQUER OITO METODOS OBRIGATÓRIOS       
  mPremiado.setRewardedVideoAdListener(new RewardedVideoAdListener() {
            @Override        
     public void onRewardedVideoAdLoaded() {
                //anúncio de vídeo premiado carregado  
               Premiado.setEnabled(true);
                Toast.makeText(MainActivity.this, "anúncio de vídeo premiado Carregado", Toast.LENGTH_SHORT).show();

            }

            @Override   
          public void onRewardedVideoAdOpened() {
//anúncio de vídeo premiado aberto               
  Toast.makeText(MainActivity.this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
            }

            @Override        
     public void onRewardedVideoStarted() {
//vídeo premiado iniciado      
           Toast.makeText(MainActivity.this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
            }

            @Override        
     public void onRewardedVideoAdClosed() {
//anúncio de vídeo premiado fechado 
                Toast.makeText(MainActivity.this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
                mPremiado.loadAd(mPremiadoTESTE, new AdRequest.Builder().build());  /* <--     Id PREMIADO no AdMbo        */            }

            @SuppressLint("SetTextI18n")
            @Override 
            public void onRewarded(RewardItem rewardItem) {
//recompensa                
               Toast.makeText(MainActivity.this, "onRewarded : "+rewardItem, Toast.LENGTH_SHORT).show();

                Premiado.setText("Premiado"+"\nRecompensa: "+String.valueOf(Premio++));
                Premiado.setEnabled(false);
                /* <--     Id PREMIADO no AdMbo        */


            }

            @Override     
        public void onRewardedVideoAdLeftApplication() {
//anúncio de vídeo premiado fechado  
               Toast.makeText(MainActivity.this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show();
            }

            @Override        
     public void onRewardedVideoAdFailedToLoad(int i) {
//anúncio de vídeo premiado falhou ao carregar                
               Toast.makeText(MainActivity.this, "VideoAdFailedToLoad : "+i, Toast.LENGTH_SHORT).show();

                if(mPremiado.isLoaded()){
                    Toast.makeText(MainActivity.this, "anúncio de vídeo premiado Carregado", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "anúncio de vídeo premiado falhou ao carregar", Toast.LENGTH_SHORT).show();
                    mPremiado.loadAd(mPremiadoTESTE, new AdRequest.Builder().build());  /* <--     Id PREMIADO no AdMbo        */              
  }
            }

            @Override    
         public void onRewardedVideoCompleted() {
//vídeo premiado concluído
            }
        });
    }


    @SuppressWarnings("unchecked")
    private <T> T Id(int id) {
        return (T) findViewById(id);
    }


    @Override  
   public void onBackPressed() {
        mPremiado.destroy(this);
        finish();
    }

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

    @Override  
   public void onPause() {
        super.onPause();
    }

    @Override   
  public void onDestroy() {
        mPremiado.destroy(this);
        super.onDestroy();
    }


}