BannerSample.java
package com.google.example.gms.ads.banner; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; import android.app.Activity; import android.os.Bundle; import android.widget.LinearLayout; /** * A simple {@link Activity} that embeds an AdView. */ public class BannerSample extends Activity { /** The view to show the ad. */ private AdView adView; /* Your ad unit id. Replace with your actual ad unit id. */ private static final String AD_UNIT_ID = "INSERT_YOUR_AD_UNIT_ID_HERE"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Create an ad. adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId(AD_UNIT_ID); // Add the AdView to the view hierarchy. The view will have no size // until the ad is loaded. LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout); layout.addView(adView); // Create an ad request. Check logcat output for the hashed device ID to // get test ads on a physical device. AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE") .build(); // Start loading the ad in the background. adView.loadAd(adRequest); } @Override public void onResume() { super.onResume(); if (adView != null) { adView.resume(); } } @Override public void onPause() { if (adView != null) { adView.pause(); } super.onPause(); } /** Called before the activity is destroyed. */ @Override public void onDestroy() { // Destroy the AdView. if (adView != null) { adView.destroy(); } super.onDestroy(); } }
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adUnitId="MY_AD_UNIT_ID" ads:adSize="BANNER"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> <activity android:label="@string/app_name" android:name="BannerExample"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> </application> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> </manifest>
jika aplikasi sudah dipublish hapuslah .addTestDevice
"The implementation for test ads and live ads are virtually the same. The only lines you should add/remove to get test/live ads are:
AdRequest.addTestDevice(AdRequest.TEST_EMULATOR); AdRequest.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE");
The first line will give you test ads on the emulator, the second would give you test ads on a specific device, provided you put in the correct device id, which you could get from the logcat output. If these lines are removed from the code, live ads will be served. Note it is good practice to use test ads during development." sumber=stackoverflow
0 komentar:
Post a Comment