facebook twitter hatena line google mixi email

Android/広告組込/Adcolony

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

動画リワード

googleの広告idを組み込んである

android/広告組込/google広告id [ショートカット]

MainActivity.java

package com.example.adcolonydemo.adcolonydemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.jirbo.adcolony.*;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
   private String app_id = "appxxxxxxxxxxxxxxxxxx";
   private String zone_id = "vzexxxxxxxxxxxxxxxxx";
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       new Thread(new Runnable() {
           @Override
           public void run() {
               AdvertisingIdClient.Info info = null;
               try {
                   info = AdvertisingIdClient.getAdvertisingIdInfo(MainActivity.this.getApplicationContext());
               } catch (IOException e) {
                   e.printStackTrace();
               } catch (GooglePlayServicesNotAvailableException e) {
                   e.printStackTrace();
               } catch (GooglePlayServicesRepairableException e) {
                   e.printStackTrace();
               }
               String advertiginID = info.getId();
               boolean limitAdTrackingEnabled = info.isLimitAdTrackingEnabled();
               Log.i("test", "広告id advertiginID=" + advertiginID); // 0d5a44d9-ad30-4717-b726-b756496xxxxx
               Log.i("test", "広告idを使用しないフラグ limitAdTrackingEnabled=" + limitAdTrackingEnabled); // false
           }
       }).start();
       AdColony.configure(this, "version:2.1,store:google", app_id, new String[]{zone_id});
       Button btnShow = (Button) findViewById(R.id.btn_show);
       btnShow.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               AdColonyV4VCAd ad = new AdColonyV4VCAd(zone_id);
               AdColonyAdListener listener = new AdColonyAdListener() {
                   // 最後に表示される広告を閉じたとき
                   public void onAdColonyAdAttemptFinished(AdColonyAd ad) {
                       // 正常
                       if(ad.shown()) {
                           Log.i("test", "onAdColonyAdAttemptFinished ad.shown()");
                       }
                       // 広告取得に失敗した時
                       if(ad.notShown()) {
                           Log.i("test", "onAdColonyAdAttemptFinished ad.notShown()");
                       }
                       if(ad.skipped()) {
                           Log.i("test", "onAdColonyAdAttemptFinished ad.skipped()");
                       }
                       // 広告がキャンセルされた時
                       if(ad.canceled()) {
                           Log.i("test", "onAdColonyAdAttemptFinished ad.canceled()");
                       }
                       // 広告が無い時
                       if(ad.noFill()) {
                           Log.i("test", "onAdColonyAdAttemptFinished ad.noFill()");
                       }
                   }
                   @Override
                   public void onAdColonyAdStarted(AdColonyAd ad) {
                       Log.i("test", "onAdColonyAdStarted");
                   }
               };
               ad.withListener(listener);
               // 準備ができたらtrue
               Log.i("test", "ad.isReady()=" + ad.isReady());
               // 準備ができたら1
               Log.i("test", "ad.getAvailableViews()=" + ad.getAvailableViews());
               // 準備ができたらactive
               Log.i("test", "ad.statusForZone()=" + AdColony.statusForZone(zone_id));
               ad.show();
           }
       });
       Button btnPreload = (Button) findViewById(R.id.btn_preload);
       btnPreload.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               AdColonyV4VCAd ad = new AdColonyV4VCAd(zone_id);
               // 準備ができたらtrue
               Log.i("test", "ad.isReady()=" + ad.isReady());
               // 準備ができたら1
               Log.i("test", "ad.getAvailableViews()=" + ad.getAvailableViews());
               // 準備ができたらactive
               Log.i("test", "ad.statusForZone()=" + AdColony.statusForZone(zone_id));
           }
       });
       AdColonyV4VCListener listener = new AdColonyV4VCListener()
       {
           public void onAdColonyV4VCReward(AdColonyV4VCReward reward)
           {
               // 動画広告が終わった時
               if(reward.success())
               {
                   Log.i("test", "AdColonyV4VCListener success()");
               }
           }
       };
       AdColony.addV4VCListener(listener);
       AdColonyAdAvailabilityListener availabilityListener = new AdColonyAdAvailabilityListener() {
           // 広告使用可能の変更を通知(init直後 or 広告開始直後にイベント発生 or 広告が無い状態の時、約10分後にイベント発生)
           public void onAdColonyAdAvailabilityChange(boolean available, String zoneId) {
               Log.i("test", "onAdColonyAdAvailabilityChange available=" + available + " zoneId=" + zoneId);
           }
       };
       AdColony.addAdAvailabilityListener(availabilityListener);
   }
   public void onPause()
   {
       super.onPause();
       AdColony.pause();
   }
   public void onResume()
   {
       super.onResume();
       AdColony.resume( this );
   }
}

res/values/version.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="google_play_services_version">6587000</integer>
</resources>

res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/activity_main"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="com.example.adcolonydemo.adcolonydemo.MainActivity">
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <Button
           android:id="@+id/btn_show"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="広告表示" />
       <Button
           android:id="@+id/btn_preload"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="確認" />
   </LinearLayout>
</RelativeLayout>

AndroidManifest.xml

       <activity
           android:name="com.jirbo.adcolony.AdColonyOverlay"
           android:configChanges="keyboardHidden|orientation|screenSize"
           android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
       <activity
           android:name="com.jirbo.adcolony.AdColonyFullscreen"
           android:configChanges="keyboardHidden|orientation|screenSize"
           android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" />
       <activity
           android:name="com.jirbo.adcolony.AdColonyBrowser"
           android:configChanges="keyboardHidden|orientation|screenSize"
           android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" />

onAdColonyAdAvailabilityChangeは動作しない場合があるようなので、isReady()などで広告有り無しを判断したほうが良い。