Monday, March 11, 2019

Android开发笔记-ch3.4.5 AD and analyze


3.4.5 AD and analyze

Refer to Google ADS, or directly go to this. As mentioned here, there are Banners and Interstitials(which occupy the whole UI) AD.
1. Interstitial AD. To me, this is a little annoying.
1) Construct an InterstitialAd object and set its ad unit ID:
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
  ...
 mInterstitialAd = new InterstitialAd(this);
 mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
  ...
}
2) Request an ad
AdRequest adRequest = new AdRequest.Builder()
 .addTestDevice("YOUR_DEVICE_HASH")
 .build();
mInterstitialAd.loadAd(adRequest);
3) Check that an ad is loaded and then display it.
if (mInterstitialAd.isLoaded()) {
 mInterstitialAd.show();
}
2. Banner
1) Need update layout xml:
    xmlns:ads="http://schemas.android.com/apk/res-auto"
...
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
2)
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice("YOUR_DEVICE_HASH")
    .build();
mAdView.loadAd(adRequest); 
 
To monetize a new app with admob, follow this link, sign in https://apps.admob.com, select app and create Ad unit ID.
For In-App Promotions, refer to this. To analyze usage, follow this guide.
May get error on some phone but not other: W/Ads: 
 Invoke Firebase method getInstance error.  java.lang.ClassNotFoundException:
 Didn't find class "com.google.android.gms.measurement.AppMeasurement" on path: ... 
According to this SO, need to force user to update the Google playservice to latest version. Refer this for how to check.


0 Comments:

Post a Comment