Skip to content

Instantly share code, notes, and snippets.

@RicardoBelchior
Created June 29, 2018 08:18
Show Gist options
  • Save RicardoBelchior/743eed3eba55a8cfae06c49083162e74 to your computer and use it in GitHub Desktop.
Save RicardoBelchior/743eed3eba55a8cfae06c49083162e74 to your computer and use it in GitHub Desktop.
PublisherAdView sample
/*
* Copyright (C) 2013 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.gms.example.bannerexample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.doubleclick.PublisherAdView;
/**
* Main Activity. Inflates main activity xml and child fragments.
*/
public class MyActivity extends AppCompatActivity {
private static final String TAG = "MyActivity";
private PublisherAdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Log.d(TAG, "#onCreate: begin");
adView = findViewById(R.id.ad_view);
final PublisherAdRequest adRequest = createArticleAdBannerRequest();
final long ts = System.currentTimeMillis();
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.i(TAG, String.format("onAdLoaded after %sms", (System.currentTimeMillis() - ts)));
}
});
adView.loadAd(adRequest);
Log.i(TAG, String.format("loadAds took: %sms", (System.currentTimeMillis() - ts)));
Log.d(TAG, "#onCreate: end");
}
/**
* Called when leaving the activity
*/
@Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
/**
* Called when returning to the activity
*/
@Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
/**
* Called before the activity is destroyed
*/
@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
/////////////////////////////////////////////////////////////////////////////////////////////
public static PublisherAdRequest createArticleAdBannerRequest() {
return new PublisherAdRequest.Builder().build();
}
/////////////////////////////////////////////////////////////////////////////////////////////
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment